<?php

namespace App\Models;

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

class VehicleInspection extends Model
{
    /** @use HasFactory<\Database\Factories\VehicleInspectionFactory> */
    use HasFactory;

    protected $fillable = [
        'job_card_id',
        'service_order_id',
        'vehicle_id',
        'inspector_id',
        'inspection_type', // 'incoming', 'outgoing'
        'current_mileage',
        'fuel_level',
        'inspection_checklist',
        'photos',
        'videos',
        'overall_condition',
        'recommendations',
        'discrepancies_found',
        'damage_notes',
        'cleanliness_rating',
        'inspection_date',
        'signature_inspector',
        'signature_customer',
        'notes',
        'additional_comments',
        'follow_up_required',
        'quality_rating',
        'damage_diagram_data',
    ];

    protected $casts = [
        'inspection_checklist' => 'array',
        'photos' => 'array',
        'videos' => 'array',
        'recommendations' => 'array',
        'discrepancies_found' => 'array',
        'damage_diagram_data' => 'array',
        'inspection_date' => 'datetime',
        'follow_up_required' => 'boolean',
    ];

    public function jobCard(): BelongsTo
    {
        return $this->belongsTo(JobCard::class);
    }

    public function serviceOrder(): BelongsTo
    {
        return $this->belongsTo(ServiceOrder::class);
    }

    public function vehicle(): BelongsTo
    {
        return $this->belongsTo(Vehicle::class);
    }

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

    public function scopeIncoming($query)
    {
        return $query->where('inspection_type', 'incoming');
    }

    public function scopeOutgoing($query)
    {
        return $query->where('inspection_type', 'outgoing');
    }

    public function compareWithOtherInspection(VehicleInspection $otherInspection): array
    {
        $differences = [];

        if ($this->overall_condition !== $otherInspection->overall_condition) {
            $differences['overall_condition'] = [
                'before' => $otherInspection->overall_condition,
                'after' => $this->overall_condition,
            ];
        }

        // Add more comparison logic as needed

        return $differences;
    }
}
