<?php

namespace Database\Factories;

use App\Models\JobCard;
use App\Models\Customer;
use App\Models\Vehicle;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\JobCard>
 */
class JobCardFactory extends Factory
{
    protected $model = JobCard::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'job_card_number' => 'ACC/' . str_pad($this->faker->numberBetween(1, 99999), 5, '0', STR_PAD_LEFT),
            'branch_code' => $this->faker->randomElement(['ACC', 'KSI', 'NBO']),
            'customer_id' => Customer::factory(),
            'vehicle_id' => Vehicle::factory(),
            'arrival_datetime' => $this->faker->dateTimeBetween('-30 days', 'now'),
            'expected_completion_date' => $this->faker->dateTimeBetween('now', '+7 days'),
            'mileage_in' => $this->faker->numberBetween(10000, 200000),
            'mileage_out' => null,
            'fuel_level_in' => $this->faker->randomElement(['full', 'three_quarters', 'half', 'quarter', 'empty']),
            'fuel_level_out' => null,
            'status' => $this->faker->randomElement([
                JobCard::STATUS_RECEIVED,
                JobCard::STATUS_INSPECTED,
                JobCard::STATUS_ASSIGNED_FOR_DIAGNOSIS,
                JobCard::STATUS_IN_DIAGNOSIS,
                JobCard::STATUS_ESTIMATE_SENT,
            ]),
            'priority' => $this->faker->randomElement(['low', 'medium', 'high', 'urgent']),
            'service_advisor_id' => User::factory(),
            'notes' => $this->faker->optional()->paragraph(),
            'customer_reported_issues' => $this->faker->sentence(),
            'vehicle_condition_notes' => $this->faker->optional()->paragraph(),
            'keys_location' => $this->faker->randomElement(['service_desk', 'ignition', 'customer', 'other']),
            'personal_items_removed' => $this->faker->boolean(),
            'photos_taken' => $this->faker->boolean(),
            'completion_datetime' => null,
            'delivery_method' => null,
            'customer_satisfaction_rating' => null,
            'delivered_by_id' => null,
            'delivery_notes' => null,
            'archived_at' => null,
            'incoming_inspection_data' => null,
            'outgoing_inspection_data' => null,
            'quality_alerts' => null,
        ];
    }

    /**
     * Create a completed job card
     */
    public function completed(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => JobCard::STATUS_COMPLETED,
                'completion_datetime' => $this->faker->dateTimeBetween('-7 days', 'now'),
                'mileage_out' => $attributes['mileage_in'] + $this->faker->numberBetween(10, 100),
                'fuel_level_out' => $this->faker->randomElement(['full', 'three_quarters', 'half']),
            ];
        });
    }

    /**
     * Create a delivered job card
     */
    public function delivered(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => JobCard::STATUS_DELIVERED,
                'completion_datetime' => $this->faker->dateTimeBetween('-7 days', '-1 day'),
                'delivery_method' => $this->faker->randomElement(['pickup', 'delivery']),
                'customer_satisfaction_rating' => $this->faker->numberBetween(1, 5),
                'delivered_by_id' => User::factory(),
                'delivery_notes' => $this->faker->optional()->paragraph(),
                'mileage_out' => $attributes['mileage_in'] + $this->faker->numberBetween(10, 100),
                'fuel_level_out' => $this->faker->randomElement(['full', 'three_quarters', 'half']),
            ];
        });
    }
}
