<?php

namespace App\Models;

use App\Domain\Account\Models\Account;
use App\Domain\Reseller\Models\ResellerLimit;
use App\Domain\Reseller\Models\ResellerSubPackage;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
// Laravel Sanctum: kurulduğunda etkinleşir (composer require laravel/sanctum)
// use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Spatie\Permission\Traits\HasRoles;

/**
 * @property int $id
 * @property string $name
 * @property string $email
 * @property string|null $two_factor_secret
 * @property string|null $two_factor_recovery_codes
 * @property \DateTimeInterface|null $two_factor_confirmed_at
 *
 * Reseller branding (nullable — sadece bayi rolündeki kullanıcılarda dolu)
 * @property string|null $reseller_brand_name
 * @property string|null $reseller_logo_url
 * @property string|null $reseller_primary_color
 * @property string|null $reseller_support_email
 * @property string|null $reseller_support_phone
 * @property string|null $reseller_white_label_hostname
 * @property-read Collection<int, Account> $ownedAccounts
 * @property-read Collection<int, ResellerSubPackage> $subPackages
 */
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, HasRoles, Notifiable, TwoFactorAuthenticatable;
    // HasApiTokens kaldırıldı — Sanctum kurulduktan sonra `use HasApiTokens` tekrar eklenecek

    protected $fillable = [
        'name',
        'email',
        'password',
        'reseller_brand_name',
        'reseller_logo_url',
        'reseller_primary_color',
        'reseller_support_email',
        'reseller_support_phone',
        'reseller_white_label_hostname',
    ];

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

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

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

    public function ownedAccounts(): HasMany
    {
        return $this->hasMany(Account::class, 'owner_user_id');
    }

    public function subPackages(): HasMany
    {
        return $this->hasMany(ResellerSubPackage::class, 'reseller_user_id');
    }

    /**
     * Bayi kaynak limitleri (1 user → 1 reseller_limit).
     * Sadece role:reseller olan kullanıcılar için anlamlıdır.
     */
    public function resellerLimit(): HasOne
    {
        return $this->hasOne(ResellerLimit::class, 'user_id');
    }

    /**
     * End-user için birincil hesap (1 user → 1 account tipik).
     */
    public function primaryAccount(): ?Account
    {
        return $this->ownedAccounts()->latest('id')->first();
    }
}
