<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'two_factor_secret',
        'two_factor_recovery_codes',
        'two_factor_confirmed_at',
        'access_token',
        'agents',
        'profile_photo_path',
        'phone',
        'business_name',
        'whatsapp_num',
        'plan_id',
        'plan_status',
        'trial_ends_at',
        'pm_last_four',
        'cancel_url',
        'welcome_valid_until',
        'company_id',
        'expotoken',
        'expired_message',
        'soon_expired_message',
        'last_login_at',
        'role',
        'phone_number_id',
        'waba_id',
        'verify_token',
        'api_token',
    ];

    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_secret',
        'two_factor_recovery_codes',
        'access_token',
    ];

    // ── Role helpers ─────────────────────────────────────────────────────────

    /** Platform owner — full access including admin management */
    public function isSuperAdmin(): bool
    {
        return $this->role === 'superadmin';
    }

    /** Any admin-panel member (superadmin OR admin) */
    public function isAdmin(): bool
    {
        return in_array($this->role, ['superadmin', 'admin']);
    }

    /** Regular company / client user */
    public function isUser(): bool
    {
        return $this->role === 'user';
    }

    // ── Relationships ─────────────────────────────────────────────────────────

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    protected function casts(): array
    {
        return [
            'email_verified_at'        => 'datetime',
            'two_factor_confirmed_at'  => 'datetime',
            'trial_ends_at'            => 'datetime',
            'welcome_valid_until'      => 'datetime',
            'last_login_at'            => 'datetime',
            'password'                 => 'hashed',
        ];
    }
}
