<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int $id
 * @property string $native_name
 * @property string $a2_iso
 * @property string $a3_iso
 * @property int $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereA2Iso($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereA3Iso($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereNativeName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language whereUpdatedAt($value)
 *
 * @property bool $site_default
 * @property bool $admin_default
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Language whereAdminDefault($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Language whereSiteDefault($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Language active()
 *
 * @mixin \Eloquent
 */
class Language extends Model
{
    use HasFactory;

    protected $fillable = [
        'native_name',
        'a2_iso',
        'a3_iso',
        'status',
        'site_default',
        'admin_default',
    ];

    protected $casts = [
        'status' => 'boolean',
        'site_default' => 'boolean',
        'admin_default' => 'boolean',
    ];

    /**
     * Aktif dilleri getir
     */
    public function scopeActive($query)
    {
        return $query->where('status', true);
    }

    /**
     * Site varsayılan dilini getir
     */
    public static function getSiteDefault()
    {
        return static::where('site_default', true)->first();
    }

    /**
     * Site varsayılan dil kodunu getir
     */
    public static function getSiteDefaultCode()
    {
        $defaultLanguage = static::getSiteDefault();

        return $defaultLanguage ? $defaultLanguage->a2_iso : 'tr'; // Fallback olarak tr
    }

    /**
     * Admin varsayılan dilini getir
     */
    public static function getAdminDefault()
    {
        return static::where('admin_default', true)->first();
    }

    /**
     * Admin varsayılan dil kodunu getir
     */
    public static function getAdminDefaultCode()
    {
        $defaultLanguage = static::getAdminDefault();

        return $defaultLanguage ? $defaultLanguage->a2_iso : 'en'; // Fallback olarak en
    }
}
