<?php

use App\Enums\ContactBudget;
use App\Enums\ContactService;
use App\Enums\ContactStatus;
use App\Models\ContactSubmission;

it('casts service, budget, status and event_date to their expected types', function () {
    $submission = ContactSubmission::factory()->create([
        'service' => ContactService::Event,
        'budget' => ContactBudget::From5000To15000,
        'status' => ContactStatus::Contacted,
        'event_date' => '2026-09-01',
    ]);

    expect($submission->service)->toBe(ContactService::Event)
        ->and($submission->budget)->toBe(ContactBudget::From5000To15000)
        ->and($submission->status)->toBe(ContactStatus::Contacted)
        ->and($submission->event_date)->toBeInstanceOf(Carbon\CarbonInterface::class);
});

it('scopes by status and treats null or all as unfiltered', function () {
    ContactSubmission::factory()->newSubmission()->create();
    ContactSubmission::factory()->contacted()->create();
    ContactSubmission::factory()->converted()->create();

    expect(ContactSubmission::query()->status('contacted')->count())->toBe(1)
        ->and(ContactSubmission::query()->status(null)->count())->toBe(3)
        ->and(ContactSubmission::query()->status('all')->count())->toBe(3);
});

it('orders latest first', function () {
    $older = ContactSubmission::factory()->create(['created_at' => now()->subDay()]);
    $newer = ContactSubmission::factory()->create(['created_at' => now()]);

    $ordered = ContactSubmission::query()->latestFirst()->pluck('id');

    expect($ordered->first())->toBe($newer->id)
        ->and($ordered->last())->toBe($older->id);
});

it('exposes a message preview and days old accessor', function () {
    $submission = ContactSubmission::factory()->create([
        'message' => str_repeat('a', 150),
        'created_at' => now()->subDays(3),
    ]);

    expect($submission->message_preview)->toHaveLength(103)
        ->and($submission->days_old)->toBe(3);
});
