<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Timezone extends Model
{
    protected $table = 'timezones';
    
    // Disable Laravel's default timestamps (we're using created_at_utc, updated_at_utc)
    public $timestamps = false;

    protected $fillable = [
        'iana_name',
        'display_name_en',
        'display_name_native',
        'utc_offset',
        'utc_offset_minutes',
        'observes_dst',
        'country_codes',
        'is_active',
        'is_default',
        'status',
        'created_at_utc',
        'updated_at_utc',
        'created_by',
        'updated_by',
        'deleted_at_utc',
        'deleted_by',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'is_default' => 'boolean',
        'observes_dst' => 'boolean',
        'utc_offset_minutes' => 'integer',
        'country_codes' => 'array', // JSON column
        'created_at_utc' => 'datetime',
        'updated_at_utc' => 'datetime',
        'deleted_at_utc' => 'datetime',
    ];

    /**
     * Scope: Get only active timezones
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true)
                     ->where('status', 'active');
    }

    /**
     * Scope: Get only non-deleted records (soft deletes)
     */
    public function scopeNotDeleted($query)
    {
        return $query->whereNull('deleted_at_utc');
    }

    /**
     * Get the employee who created this timezone
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get the employee who last updated this timezone
     */
    public function updater()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * Get the employee who deleted this timezone
     */
    public function deleter()
    {
        return $this->belongsTo(User::class, 'deleted_by');
    }
}
