<?php

namespace App\Models;

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

class ServiceRequest extends Model
{
    use HasFactory;

    // Types de demande (les 4 menus partenaire)
    public const TYPE_PRELEVEMENT = 'prelevement';
    public const TYPE_CONSOMMABLE = 'consommable';
    public const TYPE_RECLAMATION = 'reclamation';
    public const TYPE_RESULTAT = 'resultat';

    // Statuts du cycle de vie
    public const STATUS_EN_ATTENTE = 'en_attente';
    public const STATUS_ASSIGNE = 'assigne';
    public const STATUS_COLLECTE = 'collecte';
    public const STATUS_NON_DISPONIBLE = 'non_disponible';
    public const STATUS_LIVRE = 'livre';
    public const STATUS_TRAITE = 'traite';
    public const STATUS_ECART = 'ecart'; // divergence quantite demandee vs collectee

    protected $fillable = [
        'partner_id',
        'type',
        'status',
        'details',
        'requested_quantity',
        'collected_quantity',
        'assigned_courier_id',
        'urgency',
        'requested_slot',
        'unavailable_reason',
        'photo_path',
        'collected_at',
        'delivered_at',
    ];

    protected $casts = [
        'details' => 'array',
        'collected_at' => 'datetime',
        'delivered_at' => 'datetime',
    ];

    public function partner()
    {
        return $this->belongsTo(Partner::class);
    }

    public function courier()
    {
        return $this->belongsTo(User::class, 'assigned_courier_id');
    }

    public function statusHistories()
    {
        return $this->hasMany(StatusHistory::class)->latest();
    }

    /**
     * Enregistre un changement de statut avec historique + detection d'ecart automatique.
     */
    public function changeStatus(string $status, ?User $actor = null, ?string $comment = null): void
    {
        $this->status = $status;
        $this->save();

        $this->statusHistories()->create([
            'status' => $status,
            'changed_by' => $actor?->id,
            'comment' => $comment,
        ]);
    }

    public function hasDiscrepancy(): bool
    {
        if (is_null($this->requested_quantity) || is_null($this->collected_quantity)) {
            return false;
        }

        return $this->collected_quantity != $this->requested_quantity;
    }

    public static function types(): array
    {
        return [
            self::TYPE_PRELEVEMENT => 'Demande de prelevement',
            self::TYPE_CONSOMMABLE => 'Demande de consommable',
            self::TYPE_RECLAMATION => 'Reclamation',
            self::TYPE_RESULTAT => 'Demande de resultats',
        ];
    }

    public static function statuses(): array
    {
        return [
            self::STATUS_EN_ATTENTE => 'En attente',
            self::STATUS_ASSIGNE => 'Assigne a un coursier',
            self::STATUS_COLLECTE => 'Collecte',
            self::STATUS_NON_DISPONIBLE => 'Non disponible',
            self::STATUS_LIVRE => 'Livre au laboratoire',
            self::STATUS_TRAITE => 'Traite',
            self::STATUS_ECART => 'Ecart detecte',
        ];
    }
}
