<?php

namespace App\Models;

use App\Traits\Multitenantable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Outlet extends Model
{
    use SoftDeletes, Multitenantable;

    public $primaryKey = 'uuid';

    public $incrementing = false;

    public $keyType = 'string';

    public $imageFields = ['server_photo_url', 'server_banner_url'];

    protected $fillable = [
        'uuid',           
        'tenant_id',      
        'name', 
        'email',
        'phone_number',  
        'full_address',
        'latitude', 
        'longitude',
        'server_photo_url',
        'server_banner_url',
        'is_main_outlet',
        'is_active',
    ];

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

    protected $dates = ['deleted_at'];

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

    // Outlet ini milik Tenant mana
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class, 'tenant_id', 'uuid');
    }

    // Daftar User/Staf yang terdaftar di Outlet ini
    public function users(): HasMany
    {
        return $this->hasMany(User::class, 'outlet_id', 'uuid');
    }

    public function getServerBannerUrlAttribute($value)
    {
        if (!$value) return null;
        
        return asset('storage/' . $value);
    }
}
