<?php

namespace App\Models\System;

use App\Models\Boards\File;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Schedule extends Model
{
    use SoftDeletes;

    protected $table = 'schedules';

    protected $fillable = [
        'tenant_id',
        'title',
        'description',
        'start_date',
        'end_date',
        'start_time',
        'end_time',
        'is_all_day',
        'type',
        'is_recurring',
        'recurrence_rule',
        'color',
        'is_active',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'start_date' => 'date:Y-m-d',
        'end_date' => 'date:Y-m-d',
        'is_all_day' => 'boolean',
        'is_recurring' => 'boolean',
        'is_active' => 'boolean',
    ];

    protected $attributes = [
        'is_all_day' => true,
        'type' => 'event',
        'is_recurring' => false,
        'is_active' => true,
    ];

    public const TYPE_EVENT = 'event';

    public const TYPE_MEETING = 'meeting';

    public const TYPE_INTERVIEW = 'interview';

    public const TYPE_NOTICE = 'notice';

    public const TYPE_OTHER = 'other';

    public const TYPES = [
        self::TYPE_EVENT => '일정',
        self::TYPE_MEETING => '회의',
        self::TYPE_INTERVIEW => '면접',
        self::TYPE_NOTICE => '공지',
        self::TYPE_OTHER => '기타',
    ];

    public const TYPE_COLORS = [
        self::TYPE_EVENT => ['bg' => 'bg-emerald-50', 'text' => 'text-emerald-700', 'border' => 'border-emerald-200'],
        self::TYPE_MEETING => ['bg' => 'bg-blue-50', 'text' => 'text-blue-700', 'border' => 'border-blue-200'],
        self::TYPE_INTERVIEW => ['bg' => 'bg-violet-50', 'text' => 'text-violet-700', 'border' => 'border-violet-200'],
        self::TYPE_NOTICE => ['bg' => 'bg-amber-50', 'text' => 'text-amber-700', 'border' => 'border-amber-200'],
        self::TYPE_OTHER => ['bg' => 'bg-gray-50', 'text' => 'text-gray-700', 'border' => 'border-gray-200'],
    ];

    public function files()
    {
        return $this->hasMany(File::class, 'document_id')
            ->where('document_type', 'schedule');
    }

    public function scopeForTenant(Builder $query, int $tenantId): Builder
    {
        return $query->where(function ($q) use ($tenantId) {
            $q->where('tenant_id', $tenantId)
                ->orWhereNull('tenant_id');
        });
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    public function scopeBetweenDates(Builder $query, string $startDate, string $endDate): Builder
    {
        return $query->where(function ($q) use ($startDate, $endDate) {
            $q->where('start_date', '<=', $endDate)
                ->where(function ($inner) use ($startDate) {
                    $inner->where('end_date', '>=', $startDate)
                        ->orWhereNull('end_date');
                });
        });
    }

    public function getTypeLabelAttribute(): string
    {
        return self::TYPES[$this->type] ?? $this->type;
    }

    public function getTypeColorsAttribute(): array
    {
        return self::TYPE_COLORS[$this->type] ?? self::TYPE_COLORS[self::TYPE_OTHER];
    }

    /**
     * 인라인 스타일로 타입 색상 반환 (Tailwind JIT 미빌드 대응)
     */
    public function getTypeStyleAttribute(): string
    {
        $styles = [
            'event' => 'background:#ecfdf5;color:#047857;border-color:#a7f3d0;',
            'meeting' => 'background:#eff6ff;color:#1d4ed8;border-color:#bfdbfe;',
            'interview' => 'background:#f5f3ff;color:#6d28d9;border-color:#ddd6fe;',
            'notice' => 'background:#fffbeb;color:#b45309;border-color:#fde68a;',
            'other' => 'background:#f9fafb;color:#374151;border-color:#e5e7eb;',
        ];

        return $styles[$this->type] ?? 'background:#f0fdf4;color:#15803d;border-color:#bbf7d0;';
    }
}
