<?php

namespace App\Models;

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

class JobCard extends Model
{
    use HasFactory;

    protected $fillable = [
        'job_card_number',
        'branch_code',
        'customer_id',
        'vehicle_id',
        'arrival_datetime',
        'expected_completion_date',
        'mileage_in',
        'mileage_out',
        'fuel_level_in',
        'fuel_level_out',
        'status',
        'priority',
        'service_advisor_id',
        'notes',
        'customer_reported_issues',
        'vehicle_condition_notes',
        'keys_location',
        'personal_items_removed',
        'photos_taken',
        'completion_datetime',
        'delivery_method',
        'customer_satisfaction_rating',
    ];

    protected $casts = [
        'arrival_datetime' => 'datetime',
        'expected_completion_date' => 'datetime',
        'completion_datetime' => 'datetime',
        'personal_items_removed' => 'boolean',
        'photos_taken' => 'boolean',
    ];

    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($jobCard) {
            if (empty($jobCard->job_card_number)) {
                $branchCode = $jobCard->branch_code ?? config('app.default_branch_code', 'ACC');
                $nextNumber = static::where('branch_code', $branchCode)
                    ->whereYear('created_at', now()->year)
                    ->count() + 1;
                
                $jobCard->job_card_number = $branchCode . '/' . str_pad($nextNumber, 5, '0', STR_PAD_LEFT);
            }
        });
    }

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

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

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

    public function incomingInspection(): HasOne
    {
        return $this->hasOne(VehicleInspection::class)->where('inspection_type', 'incoming');
    }

    public function outgoingInspection(): HasOne
    {
        return $this->hasOne(VehicleInspection::class)->where('inspection_type', 'outgoing');
    }

    public function diagnosis(): HasOne
    {
        return $this->hasOne(Diagnosis::class);
    }

    public function workOrders(): HasMany
    {
        return $this->hasMany(WorkOrder::class);
    }

    public function timesheets(): HasMany
    {
        return $this->hasMany(Timesheet::class);
    }

    public function estimates(): HasMany
    {
        return $this->hasMany(Estimate::class);
    }

    public function scopeByBranch($query, $branchCode)
    {
        return $query->where('branch_code', $branchCode);
    }

    public function scopeByStatus($query, $status)
    {
        return $query->where('status', $status);
    }
}
