<?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;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'affiliate_id',
        'standing_id',
        'user_type',
        'first_name',
        'last_name',
        'business_name',
        'email',
        'password',
        'account_status', // 1 or 0
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, 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',
            'account_status' => 'int',
            'password' => 'hashed',
        ];
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function trading_years()
    {
        return $this->hasMany(TradingYear::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function vehicles()
    {
        return $this->hasMany(Vehicle::class);
    }



    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function profile_setting()
    {
        return $this->hasOne(ProfileSetting::class, 'user_id');
    }


    public function idDocuments()
    {
        return $this->hasMany(IdDocument::class);
    }

    // New method to get verified ID documents
    public function verifiedIdDocuments()
    {
        return $this->hasMany(IdDocument::class)->where('verified', 1); // Assuming 1 means verified
    }


    public function getCurrentTradingYearIdAttribute()
    {
        $tradingYear = TradingYear::where('user_id', $this->id)
            ->where('active_trading_year', 1)
            ->where('finalise_year', 0)
            ->first();

        return $tradingYear ? $tradingYear->id : null;
    }

    public function getCurrentTradingYearAttribute()
    {
        $tradingYear = TradingYear::where('user_id', $this->id)
            ->where('active_trading_year', 1)
            ->where('finalise_year', 0)
            ->first();

        return $tradingYear ? $tradingYear->tradingYear : null;
    }
}