<?php

namespace App\Models;

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

class EtimsConfig extends Model
{
    use HasFactory;

    protected $fillable = [
        'company_name',
        'company_npwp',
        'company_nik',
        'company_address',
        'company_phone',
        'company_email',
        'branches_id',
        'api_url',
        'api_key',
        'api_secret',
        'client_id',
        'client_secret',
        'machine_id',
        'register_id',
        'serial_number',
        'is_production',
        'is_active',
        'notes',
    ];

    protected function casts(): array
    {
        return [
            'is_production' => 'boolean',
            'is_active' => 'boolean',
        ];
    }

    public static function getActiveConfig()
    {
        return self::where('is_active', true)->first();
    }

    public static function isConfigured(): bool
    {
        $config = self::getActiveConfig();

        return $config && $config->api_url && $config->client_id && $config->client_secret;
    }

    public function getApiBaseUrl(): string
    {
        return $this->is_production
            ? ($this->api_url ?: 'https://etims-api.kemenkeu.go.id')
            : ($this->api_url ?: 'https://etims-api-sit.kemenkeu.go.id');
    }
}
