<?php

namespace App\Models\DevTools;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Crypt;

/**
 * API 환경 설정 모델
 *
 * @property int $id
 * @property int $user_id
 * @property string $name
 * @property string $base_url
 * @property string|null $api_key
 * @property string|null $auth_token
 * @property array|null $variables
 * @property bool $is_default
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 */
class ApiEnvironment extends Model
{
    protected $table = 'admin_api_environments';

    protected $fillable = [
        'user_id',
        'name',
        'base_url',
        'api_key',
        'auth_token',
        'variables',
        'is_default',
    ];

    protected $casts = [
        'variables' => 'array',
        'is_default' => 'boolean',
    ];

    /**
     * 민감 정보 숨김
     */
    protected $hidden = [
        'api_key',
        'auth_token',
    ];

    /**
     * 사용자 관계
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * API Key 암호화 저장
     */
    public function setApiKeyAttribute(?string $value): void
    {
        if ($value && config('api-explorer.security.encrypt_tokens', true)) {
            $this->attributes['api_key'] = Crypt::encryptString($value);
        } else {
            $this->attributes['api_key'] = $value;
        }
    }

    /**
     * API Key 복호화 조회
     */
    public function getDecryptedApiKeyAttribute(): ?string
    {
        if (! $this->attributes['api_key']) {
            return null;
        }

        if (config('api-explorer.security.encrypt_tokens', true)) {
            try {
                return Crypt::decryptString($this->attributes['api_key']);
            } catch (\Exception $e) {
                return $this->attributes['api_key'];
            }
        }

        return $this->attributes['api_key'];
    }

    /**
     * Auth Token 암호화 저장
     */
    public function setAuthTokenAttribute(?string $value): void
    {
        if ($value && config('api-explorer.security.encrypt_tokens', true)) {
            $this->attributes['auth_token'] = Crypt::encryptString($value);
        } else {
            $this->attributes['auth_token'] = $value;
        }
    }

    /**
     * Auth Token 복호화 조회
     */
    public function getDecryptedAuthTokenAttribute(): ?string
    {
        if (! $this->attributes['auth_token']) {
            return null;
        }

        if (config('api-explorer.security.encrypt_tokens', true)) {
            try {
                return Crypt::decryptString($this->attributes['auth_token']);
            } catch (\Exception $e) {
                return $this->attributes['auth_token'];
            }
        }

        return $this->attributes['auth_token'];
    }

    /**
     * 기본 환경 스코프
     */
    public function scopeDefault($query)
    {
        return $query->where('is_default', true);
    }
}
