<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */

    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'phone',
        'password',
        'role_type',
        'profile_photo',
        'status',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    /**
     * Get the properties owned by the user (if landlord)
     */
    public function properties()
    {
        return $this->hasMany(Property::class, 'landlord_id');
    }

    /**
     * Get bookings where user is the tenant
     */
    public function tenantBookings()
    {
        return $this->hasMany(Booking::class, 'tenant_id');
    }

    /**
     * Get bookings where user is the landlord
     */
    public function landlordBookings()
    {
        return $this->hasMany(Booking::class, 'landlord_id');
    }

    /**
     * Get rental agreements where user is the tenant
     */
    public function tenantRentalAgreements()
    {
        return $this->hasMany(RentalAgreement::class, 'tenant_id');
    }

    /**
     * Get rental agreements where user is the landlord
     */
    public function landlordRentalAgreements()
    {
        return $this->hasMany(RentalAgreement::class, 'landlord_id');
    }

    /**
     * Get payments where user is the tenant
     */
    public function tenantPayments()
    {
        return $this->hasMany(Payment::class, 'tenant_id');
    }

    /**
     * Get payments where user is the landlord
     */
    public function landlordPayments()
    {
        return $this->hasMany(Payment::class, 'landlord_id');
    }

    /**
     * Get the API keys owned by the user
     */
    public function apiKeys()
    {
        return $this->hasMany(ApiKey::class);
    }

    /**
     * Get the API logs for this user
     */
    public function apiLogs()
    {
        return $this->hasMany(ApiLog::class);
    }

    /**
     * Get reviews written by this user
     */
    public function reviews()
    {
        return $this->hasMany(Review::class, 'reviewer_id');
    }

    /**
     * Get reports made by this user
     */
    public function reports()
    {
        return $this->hasMany(Report::class, 'reporter_id');
    }

    /**
     * Get property verifications performed by this user
     */
    public function propertyVerifications()
    {
        return $this->hasMany(PropertyVerification::class, 'verified_by');
    }

    /**
     * Get messages sent by this user
     */
    public function sentMessages()
    {
        return $this->hasMany(Message::class, 'sender_id');
    }

    /**
     * Get messages received by this user
     */
    public function receivedMessages()
    {
        return $this->hasMany(Message::class, 'receiver_id');
    }

    /**
     * Get notifications for this user
     */
    public function notifications()
    {
        return $this->hasMany(Notification::class);
    }
}
