<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class RentalAgreement extends Model
{
    protected $fillable = [
        'booking_id',
        'property_id',
        'tenant_id',
        'landlord_id',
        'start_date',
        'end_date',
        'monthly_rent',
        'deposit_amount',
        'contract_document_url',
        'is_active',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'monthly_rent' => 'decimal:2',
        'deposit_amount' => 'decimal:2',
        'is_active' => 'boolean',
    ];

    // Relationships
    public function booking(): BelongsTo
    {
        return $this->belongsTo(Booking::class);
    }

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

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

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

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

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

    public function scopeByTenant($query, $tenantId)
    {
        return $query->where('tenant_id', $tenantId);
    }

    public function scopeByLandlord($query, $landlordId)
    {
        return $query->where('landlord_id', $landlordId);
    }

    public function scopeByProperty($query, $propertyId)
    {
        return $query->where('property_id', $propertyId);
    }

    public function scopeCurrent($query)
    {
        $today = now()->toDateString();
        return $query->where('is_active', true)
                    ->where(function ($q) use ($today) {
                        $q->whereNull('end_date')
                          ->orWhere('end_date', '>=', $today);
                    });
    }

    public function scopeExpired($query)
    {
        $today = now()->toDateString();
        return $query->where('is_active', true)
                    ->whereNotNull('end_date')
                    ->where('end_date', '<', $today);
    }

    // Helper methods
    public function isActive(): bool
    {
        return $this->is_active;
    }

    public function isExpired(): bool
    {
        return $this->end_date && $this->end_date < now()->toDateString();
    }

    public function isCurrent(): bool
    {
        return $this->is_active && (!$this->end_date || $this->end_date >= now()->toDateString());
    }

    public function getDurationInDays(): ?int
    {
        if (!$this->end_date) {
            return null; // Indefinite
        }

        return $this->start_date->diffInDays($this->end_date);
    }

    public function getDurationInMonths(): ?int
    {
        if (!$this->end_date) {
            return null; // Indefinite
        }

        return $this->start_date->diffInMonths($this->end_date);
    }
}
