<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable, SoftDeletes;

    protected $fillable = [
        'name',
        'email',
        'phone',
        'company',
        'role',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
        'password'          => 'hashed',
    ];

    // ============================================================
    // Role helpers
    // ============================================================
    public function hasRole(string $role): bool
    {
        return $this->role === $role;
    }

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function isMediaOwner(): bool
    {
        return $this->role === 'media_owner';
    }

    public function isAdvertiser(): bool
    {
        return in_array($this->role, ['advertiser', 'agency']);
    }

    public function isFinance(): bool
    {
        return $this->role === 'finance';
    }

    public function isVerifier(): bool
    {
        return $this->role === 'verifier';
    }

    // ============================================================
    // Relationships
    // ============================================================
    public function organisation()
    {
        return $this->hasOne(Organisation::class, 'owner_user_id');
    }

    public function mediaOwner()
    {
        return $this->hasOne(MediaOwner::class);
    }

    public function bookings()
    {
        return $this->hasMany(Booking::class);
    }

    public function carts()
    {
        return $this->hasMany(Cart::class);
    }

    public function activeCart()
    {
        return $this->hasOne(Cart::class)->where('status', 'active')->latest();
    }

    public function payments()
    {
        return $this->hasMany(Payment::class);
    }

    public function disputes()
    {
        return $this->hasMany(Dispute::class, 'raised_by');
    }
}