<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $fillable = [
        'role',
        // 'sub_sektor',
        'full_name',
        'nik',
        'birthdate',
        'address',
        'phone_number',
        'email',
        'password',
    ];

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

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

    public function pemerintahDetails()
    {
        return $this->hasOne(PemerintahDetails::class);
    }

    public function umkmDetails()
    {
        return $this->hasOne(UmkmDetails::class);
    }

    public function investorDetails()
    {
        return $this->hasOne(InvestorDetails::class);
    }

    public function akademisiDetails()
    {
        return $this->hasOne(AkademisiDetails::class);
    }

    public function komunitasDetails()
    {
        return $this->hasOne(KomunitasDetails::class);
    }

    public function personalDetails()
    {
        return $this->hasOneThrough(
            InvestorPersonalDetails::class,
            InvestorDetails::class,
            'user_id', // Foreign key on investor_details table...
            'investor_details_id', // Foreign key on investor_personal_details table...
            'id', // Local key on users table...
            'id' // Local key on investor_details table...
        );
    }

    public function perusahaanDetails()
    {
        return $this->hasOneThrough(
            InvestorPerusahaanDetails::class,
            InvestorDetails::class,
            'user_id', // Foreign key on investor_details table...
            'investor_details_id', // Foreign key on investor_perusahaan_details table...
            'id', // Local key on users table...
            'id' // Local key on investor_details table...
        );
    }

    // Method untuk mendapatkan foto berdasarkan peran
    public function getFotoAttribute()
    {
        switch ($this->role) {
            case 'pemerintah':
                return $this->pemerintahDetails->foto ?? null;
            case 'umkm':
                return $this->umkmDetails->foto ?? null;
            case 'investor':
                return $this->investorDetails->foto ?? null;
            case 'akademisi':
                return $this->akademisiDetails->foto ?? null;
            case 'komunitas':
                return $this->komunitasDetails->foto ?? null;
            default:
                return null;
        }
    }
}
