<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

// ── Note ──────────────────────────────────────────────────────────
// This model is named 'Cases' (plural) because 'Case' is a
// reserved keyword in PHP. The table name is still 'cases'.
// ──────────────────────────────────────────────────────────────────

class Cases extends Model
{
    use HasFactory;

    protected $table = 'cases';

    // ── Fillable ───────────────────────────────────────────────
    protected $fillable = [
        'hub_id',
        'created_by',
        'topic_slug',
        'topic_name_en',
        'topic_name_ar',
        'layer',
        'difficulty',
        'title_en',
        'subtitle_en',
        'situation_en',
        'challenge_questions_en',
        'solution_en',
        'bigger_lesson_en',
        'skills_taught_en',
        'title_ar',
        'subtitle_ar',
        'situation_ar',
        'challenge_questions_ar',
        'solution_ar',
        'bigger_lesson_ar',
        'skills_taught_ar',
        'case_data',
        'read_time_minutes',
        'status',
        'published_at',
        'sort_order',
    ];

    // ── Casts ─────────────────────────────────────────────────
    protected $casts = [
        'challenge_questions_en' => 'array',
        'skills_taught_en'       => 'array',
        'challenge_questions_ar' => 'array',
        'skills_taught_ar'       => 'array',
        'case_data'              => 'array',
        'published_at'           => 'datetime',
        'read_time_minutes'      => 'integer',
        'sort_order'             => 'integer',
    ];

    // ── Helpers ───────────────────────────────────────────────

    public function getTitleAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->title_ar ?? $this->title_en)
            : $this->title_en;
    }

    public function getSubtitleAttribute(): ?string
    {
        return app()->getLocale() === 'ar'
            ? ($this->subtitle_ar ?? $this->subtitle_en)
            : $this->subtitle_en;
    }

    public function getSituationAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->situation_ar ?? $this->situation_en)
            : $this->situation_en;
    }

    public function getChallengeQuestionsAttribute(): array
    {
        return app()->getLocale() === 'ar'
            ? ($this->challenge_questions_ar ?? $this->challenge_questions_en)
            : $this->challenge_questions_en;
    }

    public function getSolutionAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->solution_ar ?? $this->solution_en)
            : $this->solution_en;
    }

    public function getBiggerLessonAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->bigger_lesson_ar ?? $this->bigger_lesson_en)
            : $this->bigger_lesson_en;
    }

    public function getSkillsTaughtAttribute(): array
    {
        return app()->getLocale() === 'ar'
            ? ($this->skills_taught_ar ?? $this->skills_taught_en)
            : $this->skills_taught_en;
    }

    public function isPublished(): bool
    {
        return $this->status === 'published';
    }

    // ── Scopes ────────────────────────────────────────────────

    public function scopePublished(Builder $query): Builder
    {
        return $query->where('status', 'published');
    }

    public function scopeForHub(Builder $query, int $hubId): Builder
    {
        return $query->where('hub_id', $hubId);
    }

    public function scopeForTopic(Builder $query, string $topic): Builder
    {
        return $query->where('topic_slug', $topic);
    }

    public function scopeForDifficulty(Builder $query, string $difficulty): Builder
    {
        return $query->where('difficulty', $difficulty);
    }

    public function scopeGeneric(Builder $query): Builder
    {
        return $query->where('layer', 'generic');
    }

    public function scopeSectorSpecific(Builder $query): Builder
    {
        return $query->where('layer', 'sector_specific');
    }

    // ── Relationships ─────────────────────────────────────────

    public function hub(): BelongsTo
    {
        return $this->belongsTo(Hub::class);
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function sectors(): BelongsToMany
    {
        return $this->belongsToMany(Sector::class, 'case_sectors', 'case_id', 'sector_id');
    }

    public function progress(): HasMany
    {
        return $this->hasMany(CaseProgress::class, 'case_id');
    }

    // Progress for the currently authenticated user
    public function userProgress(): HasMany
    {
        return $this->hasMany(CaseProgress::class, 'case_id')
                    ->where('user_id', auth()->id());
    }
}
