<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Tenant extends Model
{
    public $primaryKey = 'uuid';

    public $incrementing = false;

    public $keyType = 'string';

    public $imageFields = ['server_logo_url'];

    protected $fillable = [
        'uuid',
        'business_code',
        'business_name',
        'business_email',
        'business_phone',
        'business_type',
        'server_logo_url',
        'subdomain',
        'online_store_name',
        'online_open_time',
        'online_close_time',
        'is_close_service',
        'preparation_time_minutes',
        'order_interval_minutes',
        'allow_cod',
        'max_delivery_radius_km',
        'delivery_fee_type',
        'delivery_flat_fee',
        'delivery_fee_per_km',
        'delivery_base_fee',
        'review_display_type',
        'review_display_policy',
        'subscripton_level',
        'limit_outlet',
        'subscription_expires_at',
    ];
    
    protected $casts = [
        'is_close_service' => 'boolean',
        'allow_cod' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

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

    public function users(): HasMany
    {
        return $this->hasMany(User::class, 'tenant_id', 'uuid');
    }

    public function outlets(): HasMany
    {
        return $this->hasMany(Outlet::class, 'tenant_id', 'uuid');
    }

    public function payment_methods(): HasMany
    {
        return $this->hasMany(TenantPaymentMethod::class, 'tenant_id', 'uuid');
    }
    
    public function getServerLogoUrlAttribute($value)
    {
        if (!$value) return null;
        
        return asset('storage/' . $value);
    }
}
