<?php

namespace App\Models;

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

class FoodOrder extends Model
{
    use HasFactory;

    protected $fillable = [
        'restaurant_id',
        'customer_id',
        'driver_id',
        'order_number',
        'subtotal',
        'delivery_fee',
        'tax',
        'total',
        'platform_fee',
        'platform_fee_calculated',
        'delivery_address',
        'delivery_latitude',
        'delivery_longitude',
        'status',
        'estimated_delivery_time',
        'actual_delivery_time',
        'special_instructions',
        'payment_method',
        'payment_status',
        'customer_rating',
        'customer_feedback',
        'wheel_milestone_pending',
        'wheel_promo_fee_amount',
        'wheel_result',
        'wheel_spun_at',
    ];

    protected $casts = [
        'subtotal' => 'decimal:2',
        'delivery_fee' => 'decimal:2',
        'tax' => 'decimal:2',
        'total' => 'decimal:2',
        'platform_fee' => 'decimal:2',
        'platform_fee_calculated' => 'boolean',
        'delivery_latitude' => 'decimal:8',
        'delivery_longitude' => 'decimal:8',
        'estimated_delivery_time' => 'datetime',
        'actual_delivery_time' => 'datetime',
        'customer_rating' => 'decimal:2',
        'wheel_milestone_pending' => 'boolean',
        'wheel_promo_fee_amount' => 'decimal:2',
        'wheel_result' => 'integer',
        'wheel_spun_at' => 'datetime',
    ];

    public function restaurant()
    {
        return $this->belongsTo(Restaurant::class);
    }

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

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

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

    public function orderItems()
    {
        return $this->hasMany(FoodOrderItem::class);
    }

    public function foodOrderItems()
    {
        return $this->hasMany(FoodOrderItem::class);
    }

    public function getStatusText()
    {
        $statuses = [
            'pending' => 'في الانتظار',
            'confirmed' => 'تم التأكيد',
            'preparing' => 'قيد التحضير',
            'ready' => 'جاهز للاستلام',
            'picked_up' => 'تم الاستلام',
            'delivered' => 'تم التوصيل',
            'cancelled' => 'ملغي',
        ];

        return $statuses[$this->status] ?? $this->status;
    }

    public function getStatusColor()
    {
        $colors = [
            'pending' => 'orange',
            'confirmed' => 'blue',
            'preparing' => 'purple',
            'ready' => 'green',
            'picked_up' => 'teal',
            'delivered' => 'green',
            'cancelled' => 'red',
        ];

        return $colors[$this->status] ?? 'gray';
    }

    public function getFormattedTotal()
    {
        return number_format($this->total, 0) . ' د.ع';
    }

    public function getEstimatedDeliveryTimeFormatted()
    {
        if ($this->estimated_delivery_time) {
            return $this->estimated_delivery_time->format('H:i');
        }
        return 'غير محدد';
    }

    public function canBeCancelled()
    {
        return in_array($this->status, ['pending', 'confirmed']);
    }

    public function canBeRated()
    {
        return $this->status === 'delivered' && !$this->customer_rating;
    }

    /**
     * Get platform fee record for this order.
     */
    public function platformFee()
    {
        return $this->morphOne(PlatformFee::class, 'order', 'order_type', 'order_id');
    }
}