<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * API 요청/응답 로그 모델
 *
 * @property int $id
 * @property string $method
 * @property string $url
 * @property string|null $route_name
 * @property array|null $request_headers
 * @property array|null $request_body
 * @property array|null $request_query
 * @property int $response_status
 * @property string|null $response_body
 * @property int $duration_ms
 * @property string|null $ip_address
 * @property string|null $user_agent
 * @property int|null $user_id
 * @property int|null $tenant_id
 * @property string|null $group_id
 * @property \Carbon\Carbon $created_at
 */
class ApiRequestLog extends Model
{
    protected $table = 'api_request_logs';

    public $timestamps = false;

    protected $fillable = [
        'method',
        'url',
        'route_name',
        'request_headers',
        'request_body',
        'request_query',
        'response_status',
        'response_body',
        'duration_ms',
        'ip_address',
        'user_agent',
        'user_id',
        'tenant_id',
        'group_id',
    ];

    protected $casts = [
        'request_headers' => 'array',
        'request_body' => 'array',
        'request_query' => 'array',
        'response_status' => 'integer',
        'duration_ms' => 'integer',
        'user_id' => 'integer',
        'tenant_id' => 'integer',
        'created_at' => 'datetime',
    ];

    /**
     * HTTP 메서드별 색상
     */
    public function getMethodColorAttribute(): string
    {
        return match ($this->method) {
            'GET' => 'bg-green-100 text-green-800',
            'POST' => 'bg-blue-100 text-blue-800',
            'PUT', 'PATCH' => 'bg-yellow-100 text-yellow-800',
            'DELETE' => 'bg-red-100 text-red-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }

    /**
     * 상태 코드별 색상
     */
    public function getStatusColorAttribute(): string
    {
        return match (true) {
            $this->response_status >= 500 => 'bg-red-100 text-red-800',
            $this->response_status >= 400 => 'bg-yellow-100 text-yellow-800',
            $this->response_status >= 300 => 'bg-blue-100 text-blue-800',
            $this->response_status >= 200 => 'bg-green-100 text-green-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }

    /**
     * URL에서 경로만 추출
     */
    public function getPathAttribute(): string
    {
        return parse_url($this->url, PHP_URL_PATH) ?? $this->url;
    }

    /**
     * 하루 지난 로그 삭제
     */
    public static function pruneOldLogs(): int
    {
        return static::where('created_at', '<', now()->subDay())->delete();
    }

    /**
     * 테넌트 관계
     */
    public function tenant()
    {
        return $this->belongsTo(\App\Models\Tenants\Tenant::class);
    }

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

    /**
     * AI 분석용 요약 텍스트 생성
     */
    public function getAiAnalysisSummaryAttribute(): string
    {
        $responseData = json_decode($this->response_body, true);
        $errorMessage = $responseData['message'] ?? $responseData['error'] ?? '';

        $summary = "## API 에러 분석 요청\n\n";
        $summary .= "### 요청 정보\n";
        $summary .= "- **메서드**: {$this->method}\n";
        $summary .= "- **URL**: {$this->url}\n";
        $summary .= '- **라우트**: '.($this->route_name ?? 'N/A')."\n";
        $summary .= "- **상태 코드**: {$this->response_status}\n";
        $summary .= "- **응답 시간**: {$this->duration_ms}ms\n";
        $summary .= "- **요청 시간**: {$this->created_at->format('Y-m-d H:i:s')}\n\n";

        if ($this->request_body && count($this->request_body) > 0) {
            $summary .= "### 요청 바디\n```json\n";
            $summary .= json_encode($this->request_body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            $summary .= "\n```\n\n";
        }

        $summary .= "### 응답 내용\n```json\n";
        if ($responseData) {
            $summary .= json_encode($responseData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        } else {
            $summary .= $this->response_body ?? 'N/A';
        }
        $summary .= "\n```\n\n";

        $summary .= "### 분석 요청\n";
        $summary .= "위 API 에러의 원인을 분석하고 해결 방법을 제안해주세요.\n";

        return $summary;
    }
}
