<?php

namespace Database\Factories;

use App\Models\Report;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

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

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $types = ['revenue', 'customer', 'service', 'performance'];
        $type = $this->faker->randomElement($types);

        return [
            'name' => $this->faker->words(3, true) . ' Report',
            'type' => $type,
            'data' => $this->generateDataForType($type),
            'filters' => [
                'date_from' => $this->faker->dateTimeBetween('-6 months', '-1 month')->format('Y-m-d'),
                'date_to' => $this->faker->dateTimeBetween('-1 month', 'now')->format('Y-m-d'),
            ],
            'generated_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
            'generated_by' => User::factory(),
        ];
    }

    private function generateDataForType(string $type): array
    {
        switch ($type) {
            case 'revenue':
                return [
                    'total_revenue' => $this->faker->numberBetween(10000, 100000),
                    'monthly_revenue' => collect(range(1, 12))->mapWithKeys(fn($month) => [
                        'month_' . $month => $this->faker->numberBetween(5000, 15000)
                    ])->toArray(),
                    'service_revenue' => [
                        'oil_change' => $this->faker->numberBetween(2000, 8000),
                        'brake_repair' => $this->faker->numberBetween(3000, 12000),
                        'engine_repair' => $this->faker->numberBetween(5000, 20000),
                        'transmission' => $this->faker->numberBetween(2000, 10000),
                    ],
                ];

            case 'customer':
                return [
                    'total_customers' => $this->faker->numberBetween(100, 1000),
                    'new_customers' => $this->faker->numberBetween(10, 50),
                    'retention_rate' => $this->faker->numberBetween(70, 95),
                    'customer_segments' => [
                        'new' => $this->faker->numberBetween(10, 30),
                        'regular' => $this->faker->numberBetween(40, 70),
                        'vip' => $this->faker->numberBetween(5, 20),
                    ],
                ];

            case 'service':
                return [
                    'total_services' => $this->faker->numberBetween(200, 2000),
                    'service_distribution' => [
                        'maintenance' => $this->faker->numberBetween(40, 60),
                        'repair' => $this->faker->numberBetween(30, 50),
                        'inspection' => $this->faker->numberBetween(10, 20),
                        'emergency' => $this->faker->numberBetween(5, 15),
                    ],
                    'popular_services' => [
                        'oil_change' => $this->faker->numberBetween(50, 200),
                        'brake_check' => $this->faker->numberBetween(30, 150),
                        'tire_rotation' => $this->faker->numberBetween(20, 100),
                    ],
                ];

            case 'performance':
                return [
                    'total_technicians' => $this->faker->numberBetween(5, 20),
                    'average_efficiency' => $this->faker->numberBetween(75, 95),
                    'customer_satisfaction' => $this->faker->numberBetween(80, 98),
                    'technician_performance' => collect(range(1, 5))->mapWithKeys(fn($i) => [
                        'tech_' . $i => [
                            'jobs_completed' => $this->faker->numberBetween(20, 100),
                            'efficiency' => $this->faker->numberBetween(70, 100),
                            'quality_score' => $this->faker->numberBetween(75, 100),
                        ]
                    ])->toArray(),
                ];

            default:
                return [];
        }
    }

    public function revenue(): static
    {
        return $this->state(fn () => [
            'type' => 'revenue',
            'name' => 'Revenue Analysis Report',
            'data' => $this->generateDataForType('revenue'),
        ]);
    }

    public function customer(): static
    {
        return $this->state(fn () => [
            'type' => 'customer',
            'name' => 'Customer Analytics Report',
            'data' => $this->generateDataForType('customer'),
        ]);
    }

    public function service(): static
    {
        return $this->state(fn () => [
            'type' => 'service',
            'name' => 'Service Trends Report',
            'data' => $this->generateDataForType('service'),
        ]);
    }

    public function performance(): static
    {
        return $this->state(fn () => [
            'type' => 'performance',
            'name' => 'Performance Metrics Report',
            'data' => $this->generateDataForType('performance'),
        ]);
    }
}
