<?php

namespace App\Models;

use App\Traits\Multitenantable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

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

    public $primaryKey = 'uuid';

    public $incrementing = false;

    public $keyType = 'string';

    protected $fillable = [
        'uuid',
        'tenant_id',
        'outlet_id',
        'name',
        'email', 
        'phone_number',
        'password',
        'role',
        'place_of_birth',
        'date_of_birth',
        'shift_name',
        'shift_start_time',
        'shift_end_time',
        'is_active',
        'last_sync',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'last_sync' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected $dates = ['deleted_at'];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * Relasi ke Tenant (Satu User memiliki satu Tenant)
     */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class, 'tenant_id', 'uuid');
    }

    /**
     * Relasi ke Outlet (Satu User terikat ke satu Outlet, opsional untuk Owner)
     */
    public function outlet(): BelongsTo
    {
        return $this->belongsTo(Outlet::class, 'outlet_id', 'uuid');
    }
}
