<?php

namespace App\Models;

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

class FinanceUser extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $fillable = [
        'finance_group_id',
        'username',
        'email',
        'password',
        'full_name',
        'is_active',
        'last_login_at',
        'created_by',
        'twofa_enabled',
        'twofa_secret',
        'twofa_confirmed_at',
    ];

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

    protected $casts = [
        'is_active' => 'boolean',
        'last_login_at' => 'datetime',
        'twofa_enabled' => 'boolean',
        'twofa_confirmed_at' => 'datetime',
    ];

    /**
     * Get the finance group this user belongs to
     */
    public function financeGroup()
    {
        return $this->belongsTo(FinanceGroup::class);
    }

    /**
     * Get bank accounts created by this finance user
     */
    public function bankAccounts()
    {
        return $this->hasMany(BankAccount::class);
    }

    /**
     * Get active bank accounts for this user
     */
    public function activeBankAccounts()
    {
        return $this->hasMany(BankAccount::class)->where('is_active', true);
    }

    /**
     * Get the backoffice user who created this finance user
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get activity logs for this finance user
     */
    public function activityLogs()
    {
        return $this->hasMany(ActivityLog::class);
    }
}
