<?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\Builder;
use Carbon\Carbon;

class AuctionVehicle extends Model
{
    use HasFactory;

    protected $fillable = [
        'auction_house_id',
        'lot_number',
        'slug',
        'chassis_number',
        'make',
        'model',
        'package',
        'year',
        'mileage',
        'engine_cc',
        'fuel_type',
        'transmission',
        'drive_type',
        'color',
        'auction_grade',
        'interior_grade',
        'exterior_grade',
        'starting_bid_jpy',
        'expected_bid_jpy',
        'current_highest_bid_jpy',
        'estimated_bd_cost_bdt',
        'auction_date',
        'auction_time',
        'auction_starts_at',
        'auction_ends_at',
        'main_image',
        'gallery',
        'auction_sheet_url',
        'condition_map',
        'damage_info',
        'equipment_options',
        'status',
        'result_status',
        'final_hammer_price_jpy',
        'bids_count',
        'views_count',
    ];

    protected function casts(): array
    {
        return [
            'gallery' => 'array',
            'condition_map' => 'array',
            'auction_date' => 'date',
            'auction_starts_at' => 'datetime',
            'auction_ends_at' => 'datetime',
            'starting_bid_jpy' => 'decimal:2',
            'expected_bid_jpy' => 'decimal:2',
            'current_highest_bid_jpy' => 'decimal:2',
            'estimated_bd_cost_bdt' => 'decimal:2',
            'final_hammer_price_jpy' => 'decimal:2',
            'year' => 'integer',
            'mileage' => 'integer',
            'engine_cc' => 'integer',
            'bids_count' => 'integer',
        ];
    }

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

    public function bids(): HasMany
    {
        return $this->hasMany(Bid::class)->orderBy('max_bid_jpy', 'desc');
    }

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

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

    // Scopes
    public function scopeActive(Builder $query): Builder
    {
        return $query->whereIn('status', ['upcoming', 'live']);
    }

    public function scopeLive(Builder $query): Builder
    {
        return $query->where('status', 'live');
    }

    // Helpers
    public function getFullNameAttribute(): string
    {
        return "{$this->year} {$this->make} {$this->model}" . ($this->package ? " {$this->package}" : "");
    }

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

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

    public function isBiddingOpen(): bool
    {
        if (in_array($this->status, ['ended', 'cancelled'])) {
            return false;
        }
        if ($this->auction_ends_at && Carbon::now()->greaterThanOrEqualTo($this->auction_ends_at)) {
            return false;
        }
        return true;
    }

    public function getSecondsRemainingAttribute(): int
    {
        if (!$this->auction_ends_at) {
            return 86400; // default 1 day
        }
        $diff = Carbon::now()->diffInSeconds($this->auction_ends_at, false);
        return max(0, $diff);
    }
}
