<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Contact extends Model
{
    use HasFactory, SoftDeletes, \App\Traits\HotelScopeTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'hotel_id',
        'first_name',
        'last_name',
        'type',
        'email',
        'phone',
        'company',
        'vendor_category',
        'address',
        'city',
        'country',
        'payment_terms',
        'bank_account',
        'notes',
        'active',
        'created_by',
        'updated_by',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'active' => 'boolean',
        'last_transaction_date' => 'datetime',
    ];

    /**
     * Get the invoices for the contact.
     */
    public function invoices(): HasMany
    {
        return $this->hasMany(Invoice::class);
    }

    /**
     * Get the payment vouchers for the contact (as vendor).
     */
    public function paymentVouchers(): HasMany
    {
        return $this->hasMany(PaymentVoucher::class, 'vendor_id');
    }

    /**
     * Scope a query to only include customers.
     */
    public function scopeCustomers($query)
    {
        return $query->where('type', 'customer');
    }

    /**
     * Scope a query to only include vendors.
     */
    public function scopeVendors($query)
    {
        return $query->where('type', 'vendor');
    }

    /**
     * Scope a query to only include suppliers.
     */
    public function scopeSuppliers($query)
    {
        return $query->where('type', 'supplier');
    }

    /**
     * Scope a query to only include active contacts.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Get the full address.
     */
    public function getFullAddressAttribute(): string
    {
        $parts = [];
        
        if (isset($this->address) && $this->address) $parts[] = $this->address;
        if (isset($this->city) && $this->city) $parts[] = $this->city;
        if (isset($this->country) && $this->country) $parts[] = $this->country;
        
        return implode(', ', $parts);
    }

    /**
     * Get the contact's status badge color.
     */
    public function getStatusColorAttribute(): string
    {
        if (!$this->active) {
            return 'danger';
        }
        
        return 'success';
    }

    /**
     * Get the contact's status text.
     */
    public function getStatusTextAttribute(): string
    {
        if (!$this->active) {
            return 'Inactive';
        }
        
        return 'Active';
    }

    /**
     * Update the contact's balance.
     */
    public function updateBalance(float $amount, string $type = 'add'): void
    {
        // current_balance column is currently missing from DB, 
        // so we just update the transaction date for now.
        // In a real accounting system, this should be tracked.
        $this->update([
            'updated_at' => now(),
        ]);
    }

    /**
     * Check if contact is a vendor.
     */
    public function isVendor(): bool
    {
        return in_array($this->type, ['vendor', 'supplier']);
    }

    /**
     * Check if contact is a customer.
     */
    public function isCustomer(): bool
    {
        return $this->type === 'customer';
    }

    /**
     * Get the full name attribute
     */
    public function getNameAttribute(): string
    {
        return trim("{$this->first_name} {$this->last_name}");
    }
}