<?php

namespace App\Models\Tenants;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AiReport extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id',
        'report_date',
        'report_type',
        'content',
        'summary',
        'input_data',
        'status',
        'error_message',
        'created_by',
    ];

    protected $casts = [
        'report_date' => 'date',
        'content' => 'array',
        'input_data' => 'array',
    ];

    /**
     * 리포트 유형
     */
    public const REPORT_TYPES = [
        'daily' => '일일',
        'weekly' => '주간',
        'monthly' => '월간',
    ];

    /**
     * 리포트 상태
     */
    public const STATUSES = [
        'pending' => '생성중',
        'completed' => '완료',
        'failed' => '실패',
    ];

    /**
     * 분석 영역
     */
    public const ANALYSIS_AREAS = [
        'expense' => '지출분석',
        'loan' => '가지급금',
        'card_account' => '카드/계좌',
        'receivable' => '미수금',
        'sales' => '매출분석',
        'purchase' => '매입분석',
    ];

    /**
     * 상태 코드
     */
    public const STATUS_CODES = [
        'critical' => '경고',
        'warning' => '주의',
        'positive' => '긍정',
        'normal' => '양호',
    ];

    /**
     * 생성자 관계
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
    }

    /**
     * 리포트 유형 라벨
     */
    public function getReportTypeLabelAttribute(): string
    {
        return self::REPORT_TYPES[$this->report_type] ?? $this->report_type;
    }

    /**
     * 상태 라벨
     */
    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? $this->status;
    }

    /**
     * 완료 여부
     */
    public function isCompleted(): bool
    {
        return $this->status === 'completed';
    }

    /**
     * 실패 여부
     */
    public function isFailed(): bool
    {
        return $this->status === 'failed';
    }
}
