<?php

namespace Modules\Vrc\Tests\Unit;

use Illuminate\Database\Capsule\Manager as Capsule;
use Modules\Vrc\Entities\VrcServiceReminder;
use PHPUnit\Framework\TestCase;

/**
 * §6.12: the reminder command picks up due reminders. Verifies the `due`
 * scope against an in-memory SQLite DB (the dispatch/mark-sent path is thin
 * glue over this scope + the notification contract).
 */
class ServiceReminderTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $c = new Capsule;
        $c->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '']);
        $c->setAsGlobal();
        $c->bootEloquent();

        Capsule::schema()->create('vrc_service_reminders', function ($t) {
            $t->increments('id');
            $t->integer('business_id');
            $t->integer('vehicle_id');
            $t->integer('erp_customer_id');
            $t->string('type')->default('date');
            $t->date('due_date')->nullable();
            $t->dateTime('scheduled_for')->nullable();
            $t->string('status')->default('pending');
            $t->string('channel')->default('sms');
            $t->dateTime('sent_at')->nullable();
            $t->timestamps();
        });

        VrcServiceReminder::query()->insert([
            ['business_id' => 1, 'vehicle_id' => 1, 'erp_customer_id' => 1, 'due_date' => '2026-06-01', 'status' => 'pending', 'channel' => 'sms'],
            ['business_id' => 1, 'vehicle_id' => 2, 'erp_customer_id' => 2, 'due_date' => '2027-01-01', 'status' => 'pending', 'channel' => 'sms'],
            ['business_id' => 1, 'vehicle_id' => 3, 'erp_customer_id' => 3, 'due_date' => '2026-06-01', 'status' => 'sent', 'channel' => 'sms'],
        ]);
    }

    protected function tearDown(): void
    {
        Capsule::schema()->drop('vrc_service_reminders');
        parent::tearDown();
    }

    public function test_due_scope_picks_only_pending_past_due(): void
    {
        $due = VrcServiceReminder::due('2026-06-22')->get();
        $this->assertCount(1, $due);
        $this->assertSame(1, (int) $due->first()->vehicle_id);
    }

    public function test_future_reminder_not_due(): void
    {
        $due = VrcServiceReminder::due('2026-06-22')->pluck('vehicle_id')->all();
        $this->assertNotContains(2, $due);
    }
}
