<?php

namespace App\Models\Admin;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 프로젝트 관리 - 일일 로그 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property int|null $project_id
 * @property \Carbon\Carbon $log_date
 * @property string|null $summary
 * @property int $created_by
 * @property int|null $updated_by
 * @property int|null $deleted_by
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property \Carbon\Carbon|null $deleted_at
 */
class AdminPmDailyLog extends Model
{
    use SoftDeletes;

    protected $table = 'admin_pm_daily_logs';

    protected $fillable = [
        'tenant_id',
        'project_id',
        'log_date',
        'summary',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'log_date' => 'date',
        'tenant_id' => 'integer',
        'project_id' => 'integer',
        'created_by' => 'integer',
        'updated_by' => 'integer',
        'deleted_by' => 'integer',
    ];

    /**
     * 관계: 프로젝트
     */
    public function project(): BelongsTo
    {
        return $this->belongsTo(AdminPmProject::class, 'project_id');
    }

    /**
     * 관계: 항목들
     */
    public function entries(): HasMany
    {
        return $this->hasMany(AdminPmDailyLogEntry::class, 'daily_log_id')->orderBy('sort_order');
    }

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

    /**
     * 관계: 수정자
     */
    public function updater(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * 스코프: 테넌트 필터
     */
    public function scopeTenant($query, int $tenantId)
    {
        return $query->where('tenant_id', $tenantId);
    }

    /**
     * 스코프: 날짜 범위 필터
     */
    public function scopeDateRange($query, ?string $startDate, ?string $endDate)
    {
        if ($startDate) {
            $query->where('log_date', '>=', $startDate);
        }
        if ($endDate) {
            $query->where('log_date', '<=', $endDate);
        }

        return $query;
    }

    /**
     * 스코프: 프로젝트 필터
     */
    public function scopeProject($query, ?int $projectId)
    {
        if ($projectId) {
            return $query->where('project_id', $projectId);
        }

        return $query;
    }

    /**
     * 항목 통계
     */
    public function getEntryStatsAttribute(): array
    {
        return [
            'total' => $this->entries()->count(),
            'todo' => $this->entries()->where('status', AdminPmDailyLogEntry::STATUS_TODO)->count(),
            'in_progress' => $this->entries()->where('status', AdminPmDailyLogEntry::STATUS_IN_PROGRESS)->count(),
            'done' => $this->entries()->where('status', AdminPmDailyLogEntry::STATUS_DONE)->count(),
        ];
    }

    /**
     * 프로젝트명 (없으면 '전체')
     */
    public function getProjectNameAttribute(): string
    {
        return $this->project?->name ?? '전체';
    }

    /**
     * 포맷된 날짜
     */
    public function getFormattedDateAttribute(): string
    {
        return $this->log_date->format('Y-m-d (D)');
    }
}
