<?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;

class DeliveryAssignment extends Model
{
    use HasFactory;

    protected $fillable = [
        'order_id',
        'delivery_agent_id',
        'status',
        'assigned_at',
        'accepted_at',
        'rejected_at',
        'reached_store_at',
        'picked_up_at',
        'on_the_way_at',
        'reached_customer_at',
        'delivered_at',
        'cancelled_at',
        'rejection_reason',
        'delivery_notes',
        'delivery_otp',
        'delivery_fee',
        'bonus',
    ];

    protected function casts(): array
    {
        return [
            'assigned_at' => 'datetime',
            'accepted_at' => 'datetime',
            'rejected_at' => 'datetime',
            'reached_store_at' => 'datetime',
            'picked_up_at' => 'datetime',
            'on_the_way_at' => 'datetime',
            'reached_customer_at' => 'datetime',
            'delivered_at' => 'datetime',
            'cancelled_at' => 'datetime',
            'delivery_fee' => 'decimal:2',
            'bonus' => 'decimal:2',
        ];
    }

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

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

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

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

    public function updateStatus(string $status, ?string $notes = null): void
    {
        $this->update([
            'status' => $status,
            'delivery_notes' => $notes,
            $this->getStatusTimestampField($status) => now(),
        ]);

        // Update order delivery status
        $this->order->update([
            'delivery_status' => $status,
            $this->getOrderStatusTimestampField($status) => now(),
        ]);
    }

    protected function getStatusTimestampField(string $status): string
    {
        return match($status) {
            'accepted' => 'accepted_at',
            'rejected' => 'rejected_at',
            'reached_store' => 'reached_store_at',
            'picked_up' => 'picked_up_at',
            'on_the_way' => 'on_the_way_at',
            'reached_customer' => 'reached_customer_at',
            'delivered' => 'delivered_at',
            'cancelled' => 'cancelled_at',
            default => 'assigned_at',
        };
    }

    protected function getOrderStatusTimestampField(string $status): string
    {
        return match($status) {
            'accepted' => 'accepted_at',
            'reached_store' => 'reached_store_at',
            'picked_up' => 'picked_up_at',
            'on_the_way' => 'on_the_way_at',
            'reached_customer' => 'reached_customer_at',
            'delivered' => 'delivered_at',
            default => null,
        };
    }
}
