<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

class Report extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'type',
        'data',
        'filters',
        'generated_at',
        'generated_by',
    ];

    protected $casts = [
        'data' => 'array',
        'filters' => 'array',
        'generated_at' => 'datetime',
    ];

    public function generatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'generated_by');
    }

    public static function getRevenueData($dateFrom = null, $dateTo = null): array
    {
        $dateFrom = $dateFrom ? Carbon::parse($dateFrom) : Carbon::now()->subMonths(6);
        $dateTo = $dateTo ? Carbon::parse($dateTo) : Carbon::now();

        // Return sample data since we don't have actual service_orders table structure
        return [
            'total_revenue' => 125000.50,
            'monthly_revenue' => [
                '2024-12' => 18500,
                '2025-01' => 15000,
                '2025-02' => 18000,
                '2025-03' => 22000,
                '2025-04' => 19000,
                '2025-05' => 25000,
                '2025-06' => 26500,
                '2025-07' => 28000,
            ],
            'service_revenue' => [
                'Oil Change' => 8500,
                'Brake Repair' => 15000,
                'Engine Repair' => 35000,
                'Transmission' => 12000,
                'Tire Service' => 9500,
                'Diagnostics' => 11200,
            ],
            'avg_order_value' => 285.50,
            'period' => [
                'from' => $dateFrom->format('Y-m-d'),
                'to' => $dateTo->format('Y-m-d'),
            ]
        ];
    }

    public static function getCustomerAnalytics($dateFrom = null, $dateTo = null): array
    {
        $dateFrom = $dateFrom ? Carbon::parse($dateFrom) : Carbon::now()->subMonths(6);
        $dateTo = $dateTo ? Carbon::parse($dateTo) : Carbon::now();

        return [
            'total_customers' => 542,
            'new_customers' => 47,
            'retention_rate' => 78.5,
            'customer_segments' => [
                'new' => 47,
                'regular' => 385,
                'vip' => 110,
            ],
            'avg_satisfaction' => 4.3,
            'customer_lifetime_value' => 1250.75,
            'repeat_customers' => 385,
            'top_customers' => collect([
                (object)[
                    'id' => 1,
                    'first_name' => 'John',
                    'last_name' => 'Smith',
                    'full_name' => 'John Smith',
                    'service_orders_count' => 12,
                    'total_spent' => 3500.00,
                ],
                (object)[
                    'id' => 2,
                    'first_name' => 'Sarah',
                    'last_name' => 'Johnson',
                    'full_name' => 'Sarah Johnson',
                    'service_orders_count' => 8,
                    'total_spent' => 2850.00,
                ],
                (object)[
                    'id' => 3,
                    'first_name' => 'Mike',
                    'last_name' => 'Davis',
                    'full_name' => 'Mike Davis',
                    'service_orders_count' => 6,
                    'total_spent' => 2200.00,
                ],
                (object)[
                    'id' => 4,
                    'first_name' => 'Emily',
                    'last_name' => 'Wilson',
                    'full_name' => 'Emily Wilson',
                    'service_orders_count' => 5,
                    'total_spent' => 1875.00,
                ],
                (object)[
                    'id' => 5,
                    'first_name' => 'David',
                    'last_name' => 'Brown',
                    'full_name' => 'David Brown',
                    'service_orders_count' => 4,
                    'total_spent' => 1650.00,
                ],
                (object)[
                    'id' => 6,
                    'first_name' => 'Lisa',
                    'last_name' => 'Garcia',
                    'full_name' => 'Lisa Garcia',
                    'service_orders_count' => 3,
                    'total_spent' => 1200.00,
                ],
            ]),
            'period' => [
                'from' => $dateFrom->format('Y-m-d'),
                'to' => $dateTo->format('Y-m-d'),
            ]
        ];
    }

    public static function getServiceTrends($dateFrom = null, $dateTo = null): array
    {
        $dateFrom = $dateFrom ? Carbon::parse($dateFrom) : Carbon::now()->subMonths(6);
        $dateTo = $dateTo ? Carbon::parse($dateTo) : Carbon::now();

        return [
            'total_services' => 1248,
            'service_distribution' => [
                'Maintenance' => 45,
                'Repair' => 35,
                'Inspection' => 15,
                'Emergency' => 5,
            ],
            'popular_services' => [
                'Oil Change' => 156,
                'Brake Service' => 89,
                'Tire Rotation' => 73,
                'Engine Diagnostics' => 65,
                'Transmission Service' => 41,
            ],
            'monthly_trends' => [
                '2024-12' => 185,
                '2025-01' => 185,
                '2025-02' => 198,
                '2025-03' => 225,
                '2025-04' => 201,
                '2025-05' => 216,
                '2025-06' => 223,
                '2025-07' => 240,
            ],
            'service_trends' => collect([
                (object)[
                    'service_type' => 'oil_change',
                    'count' => 156,
                    'avg_amount' => 45.50,
                    'total_revenue' => 7098.00,
                ],
                (object)[
                    'service_type' => 'brake_service',
                    'count' => 89,
                    'avg_amount' => 168.50,
                    'total_revenue' => 14996.50,
                ],
                (object)[
                    'service_type' => 'tire_rotation',
                    'count' => 73,
                    'avg_amount' => 25.00,
                    'total_revenue' => 1825.00,
                ],
                (object)[
                    'service_type' => 'engine_diagnostics',
                    'count' => 65,
                    'avg_amount' => 125.00,
                    'total_revenue' => 8125.00,
                ],
                (object)[
                    'service_type' => 'transmission_service',
                    'count' => 41,
                    'avg_amount' => 292.75,
                    'total_revenue' => 12002.75,
                ],
                (object)[
                    'service_type' => 'air_conditioning',
                    'count' => 38,
                    'avg_amount' => 85.00,
                    'total_revenue' => 3230.00,
                ],
            ]),
            'avg_service_time' => 2.5, // hours
            'period' => [
                'from' => $dateFrom->format('Y-m-d'),
                'to' => $dateTo->format('Y-m-d'),
            ]
        ];
    }

    public static function getPerformanceMetrics($dateFrom = null, $dateTo = null): array
    {
        $dateFrom = $dateFrom ? Carbon::parse($dateFrom) : Carbon::now()->subMonths(6);
        $dateTo = $dateTo ? Carbon::parse($dateTo) : Carbon::now();

        return [
            'total_technicians' => 8,
            'average_efficiency' => 87.5,
            'average_quality' => 92.3,
            'customer_satisfaction' => 4.3,
            'technician_performance' => [
                'John Smith' => [
                    'efficiency' => 94.2,
                    'quality' => 96.8,
                    'jobs_completed' => 145,
                    'customer_rating' => 4.8,
                ],
                'Mike Johnson' => [
                    'efficiency' => 89.1,
                    'quality' => 91.5,
                    'jobs_completed' => 132,
                    'customer_rating' => 4.2,
                ],
                'Sarah Davis' => [
                    'efficiency' => 92.4,
                    'quality' => 94.2,
                    'jobs_completed' => 128,
                    'customer_rating' => 4.6,
                ],
                'Tom Wilson' => [
                    'efficiency' => 85.7,
                    'quality' => 88.9,
                    'jobs_completed' => 118,
                    'customer_rating' => 4.1,
                ],
                'Lisa Brown' => [
                    'efficiency' => 88.3,
                    'quality' => 93.1,
                    'jobs_completed' => 125,
                    'customer_rating' => 4.4,
                ],
                'David Garcia' => [
                    'efficiency' => 91.7,
                    'quality' => 89.6,
                    'jobs_completed' => 138,
                    'customer_rating' => 4.3,
                ],
                'Amanda Chen' => [
                    'efficiency' => 86.9,
                    'quality' => 95.2,
                    'jobs_completed' => 142,
                    'customer_rating' => 4.7,
                ],
                'Robert Martinez' => [
                    'efficiency' => 93.1,
                    'quality' => 90.4,
                    'jobs_completed' => 156,
                    'customer_rating' => 4.5,
                ],
            ],
            'period' => [
                'from' => $dateFrom->format('Y-m-d'),
                'to' => $dateTo->format('Y-m-d'),
            ]
        ];
    }
}
