<?php

namespace App\Models;

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

class Offer extends Model
{
    use HasFactory;

    protected $fillable = [
        'item_id',
        'buyer_id',
        'seller_id',
        'offered_price',
        'is_committed',
        'status',
        'message',
        'seller_message',
        'counter_price',
        'expires_at',
        'accepted_at',
        'rejected_at',
    ];

    protected function casts(): array
    {
        return [
            'is_committed' => 'boolean',
            'expires_at' => 'datetime',
            'accepted_at' => 'datetime',
            'rejected_at' => 'datetime',
        ];
    }

    public function item()
    {
        return $this->belongsTo(Item::class);
    }

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

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

    // Scopes
    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeAccepted($query)
    {
        return $query->where('status', 'accepted');
    }

    public function scopeCounterOffered($query)
    {
        return $query->where('status', 'counter_offered');
    }

    public function scopeRejected($query)
    {
        return $query->where('status', 'rejected');
    }

    public function scopeCommitted($query)
    {
        return $query->where('is_committed', true);
    }

    public function scopeNoCommit($query)
    {
        return $query->where('is_committed', false);
    }

    public function scopeActive($query)
    {
        return $query->whereIn('status', ['pending', 'counter_offered'])
            ->where(function ($q) {
                $q->whereNull('expires_at')
                  ->orWhere('expires_at', '>', now());
            });
    }

    public function scopeExpired($query)
    {
        return $query->whereIn('status', ['pending', 'counter_offered'])
            ->where('expires_at', '<=', now());
    }

    // Check if offer is expired
    public function isExpired(): bool
    {
        return $this->expires_at && now()->isAfter($this->expires_at);
    }

    // Mark expired offers as expired
    public function markExpiredIfNeeded(): void
    {
        if ($this->isExpired() && in_array($this->status, ['pending', 'counter_offered'])) {
            $this->update(['status' => 'expired']);
        }
    }

    // Get display label for offer type
    public function getOfferTypeLabel(): string
    {
        return $this->is_committed ? 'Committed Offer' : 'No-Commit Offer';
    }

    // Get status label
    public function getStatusLabel(): string
    {
        return match($this->status) {
            'pending' => 'Pending',
            'accepted' => 'Accepted',
            'counter_offered' => 'Counter Offered',
            'rejected' => 'Rejected',
            'withdrawn' => 'Withdrawn',
            'expired' => 'Expired',
            default => $this->status,
        };
    }
}
