<?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
{
    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',
    ];

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