<?php

namespace Database\Seeders;

use App\Models\AdoptionApplication;
use App\Models\Interview;
use Illuminate\Database\Seeder;

class InterviewSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $locations = [
            'Main Shelter - Room A',
            'Main Shelter - Room B',
            'Downtown Office',
            'Virtual Meeting (Zoom)',
            'Home Visit',
        ];

        $now = now();
        $interviews = [];

        // Interview scheduled applications
        $interviewScheduledApps = AdoptionApplication::where('status', 'interview_scheduled')->pluck('id');
        foreach ($interviewScheduledApps as $applicationId) {
            $interviews[] = [
                'adoption_application_id' => $applicationId,
                'scheduled_at' => fake()->dateTimeBetween('now', '+2 weeks'),
                'location' => fake()->randomElement($locations),
                'notes' => fake()->optional()->sentence(),
                'completed_at' => null,
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        // Approved applications
        $approvedApps = AdoptionApplication::where('status', 'approved')->pluck('id');
        foreach ($approvedApps as $applicationId) {
            $interviews[] = [
                'adoption_application_id' => $applicationId,
                'scheduled_at' => fake()->dateTimeBetween('-2 weeks', '-3 days'),
                'location' => fake()->randomElement($locations),
                'notes' => 'Interview went well. Applicant shows great understanding of pet care.',
                'completed_at' => fake()->dateTimeBetween('-2 days', 'now'),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        // Completed applications
        $completedApps = AdoptionApplication::where('status', 'completed')->pluck('id');
        foreach ($completedApps as $applicationId) {
            $interviews[] = [
                'adoption_application_id' => $applicationId,
                'scheduled_at' => fake()->dateTimeBetween('-1 month', '-2 weeks'),
                'location' => fake()->randomElement($locations),
                'notes' => 'Excellent interview. Applicant is a perfect match for this pet.',
                'completed_at' => fake()->dateTimeBetween('-10 days', '-5 days'),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        // Some rejected applications that went through interview stage
        $rejectedNotes = [
            'Applicant did not show sufficient knowledge of pet care requirements.',
            'Living situation not suitable for this type of pet.',
            'Applicant was not responsive to follow-up questions.',
        ];
        $someRejectedApps = AdoptionApplication::where('status', 'rejected')
            ->whereHas('statusHistory', fn ($q) => $q->where('to_status', 'interview_scheduled'))
            ->take(2)
            ->pluck('id');

        foreach ($someRejectedApps as $applicationId) {
            $interviews[] = [
                'adoption_application_id' => $applicationId,
                'scheduled_at' => fake()->dateTimeBetween('-3 weeks', '-1 week'),
                'location' => fake()->randomElement($locations),
                'notes' => fake()->randomElement($rejectedNotes),
                'completed_at' => fake()->dateTimeBetween('-5 days', '-1 day'),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        // Cancelled applications that went through interview stage
        $cancelledNotes = [
            'Interview completed. Applicant requested to cancel after finding another pet.',
            'Interview went well but applicant\'s circumstances changed (moving to apartment that doesn\'t allow pets).',
            'Completed interview but applicant decided to wait before adopting.',
        ];
        $cancelledAppsWithInterviews = AdoptionApplication::where('status', 'cancelled')
            ->take(3)
            ->pluck('id');

        foreach ($cancelledAppsWithInterviews as $applicationId) {
            $interviews[] = [
                'adoption_application_id' => $applicationId,
                'scheduled_at' => fake()->dateTimeBetween('-3 weeks', '-1 week'),
                'location' => fake()->randomElement($locations),
                'notes' => fake()->randomElement($cancelledNotes),
                'completed_at' => fake()->dateTimeBetween('-6 days', '-2 days'),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        if (! empty($interviews)) {
            Interview::insert($interviews);
        }
    }
}
