<?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 Bid extends Model
{
    use HasFactory;

    protected $fillable = [
        'bid_code',
        'user_id',
        'auction_vehicle_id',
        'max_bid_jpy',
        'exchange_rate',
        'estimated_bd_cost_bdt',
        'currency',
        'cost_breakdown_snapshot',
        'status',
        'terms_accepted',
        'terms_accepted_at',
        'ip_address',
        'user_agent',
        'reviewed_by',
        'reviewed_at',
        'admin_notes',
        'japan_api_provider',
        'japan_api_bid_id',
        'japan_api_status',
        'japan_api_submitted_at',
        'japan_api_response',
        'won_hammer_price_jpy',
        'final_bd_cost_bdt',
    ];

    protected function casts(): array
    {
        return [
            'cost_breakdown_snapshot' => 'array',
            'japan_api_response' => 'array',
            'terms_accepted' => 'boolean',
            'terms_accepted_at' => 'datetime',
            'reviewed_at' => 'datetime',
            'japan_api_submitted_at' => 'datetime',
            'max_bid_jpy' => 'decimal:2',
            'exchange_rate' => 'decimal:4',
            'estimated_bd_cost_bdt' => 'decimal:2',
            'won_hammer_price_jpy' => 'decimal:2',
            'final_bd_cost_bdt' => 'decimal:2',
        ];
    }

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

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

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

    public function history(): HasMany
    {
        return $this->hasMany(BidHistory::class)->orderBy('created_at', 'desc');
    }

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

    // Status helpers
    public function getStatusBadgeClassAttribute(): string
    {
        return match ($this->status) {
            'pending' => 'bg-amber-100 text-amber-800 border-amber-200 dark:bg-amber-950/60 dark:text-amber-400 dark:border-amber-800/60',
            'approved' => 'bg-blue-100 text-blue-800 border-blue-200 dark:bg-blue-950/60 dark:text-blue-400 dark:border-blue-800/60',
            'submitted' => 'bg-indigo-100 text-indigo-800 border-indigo-200 dark:bg-indigo-950/60 dark:text-indigo-400 dark:border-indigo-800/60',
            'bidding' => 'bg-purple-100 text-purple-800 border-purple-200 dark:bg-purple-950/60 dark:text-purple-400 dark:border-purple-800/60',
            'won' => 'bg-emerald-100 text-emerald-800 border-emerald-200 dark:bg-emerald-950/60 dark:text-emerald-400 dark:border-emerald-800/60',
            'lost' => 'bg-rose-100 text-rose-800 border-rose-200 dark:bg-rose-950/60 dark:text-rose-400 dark:border-rose-800/60',
            'rejected' => 'bg-red-100 text-red-800 border-red-200 dark:bg-red-950/60 dark:text-red-400 dark:border-red-800/60',
            'cancelled' => 'bg-slate-100 text-slate-800 border-slate-200 dark:bg-slate-800 dark:text-slate-400 dark:border-slate-700',
            'expired' => 'bg-gray-100 text-gray-800 border-gray-200 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-700',
            default => 'bg-gray-100 text-gray-700',
        };
    }

    public function getFormattedMaxBidAttribute(): string
    {
        return '¥' . number_format($this->max_bid_jpy, 0);
    }

    public function getFormattedEstimatedCostAttribute(): string
    {
        return '৳' . number_format($this->estimated_bd_cost_bdt, 0);
    }
}
