<?php

namespace App\Http\Controllers\Modules;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;

class SchoolModuleController extends Controller
{
    public function index(Request $request): Response
    {
        $schoolId = $this->currentSchoolId($request);

        $modules = $schoolId
            ? DB::table('modules')
                ->leftJoin('school_modules', function ($join) use ($schoolId): void {
                    $join->on('school_modules.module_id', '=', 'modules.id')
                        ->where('school_modules.school_id', '=', $schoolId);
                })
                ->select([
                    'modules.key_name',
                    'modules.name_fa',
                    'modules.is_core',
                    'modules.is_sellable',
                    'school_modules.enabled',
                    'school_modules.expires_at',
                    'school_modules.price',
                ])
                ->orderByDesc('modules.is_core')
                ->orderBy('modules.name_fa')
                ->get()
            : collect();

        return Inertia::render('Modules/SchoolModules', [
            'modules' => $modules->map(fn ($module): array => [
                'keyName' => $module->key_name,
                'name' => $module->name_fa,
                'isCore' => (bool) $module->is_core,
                'isSellable' => (bool) $module->is_sellable,
                'enabled' => (bool) $module->enabled || (bool) $module->is_core,
                'expiresAt' => $module->expires_at ? $this->jalaliDate($module->expires_at) : '—',
                'price' => (float) ($module->price ?? 0),
                'uiBehavior' => ((bool) $module->enabled || (bool) $module->is_core) ? 'نمایش در منو' : 'مخفی کامل',
            ])->values(),
        ]);
    }

    public function store(Request $request): Response
    {
        $schoolId = $this->currentSchoolId($request);

        $modules = $schoolId
            ? DB::table('modules')
                ->leftJoin('school_modules', function ($join) use ($schoolId): void {
                    $join->on('school_modules.module_id', '=', 'modules.id')
                        ->where('school_modules.school_id', '=', $schoolId);
                })
                ->where('modules.is_sellable', true)
                ->where('modules.status', 'active')
                ->select([
                    'modules.id',
                    'modules.key_name',
                    'modules.name_fa',
                    'modules.description',
                    'school_modules.enabled',
                ])
                ->orderBy('modules.name_fa')
                ->get()
            : collect();

        $dependencies = DB::table('module_dependencies')
            ->join('modules as module', 'module.id', '=', 'module_dependencies.module_id')
            ->join('modules as required', 'required.id', '=', 'module_dependencies.required_module_id')
            ->select(['module.key_name as module_key', 'required.name_fa as required_name'])
            ->get()
            ->groupBy('module_key')
            ->map(fn ($items) => $items->pluck('required_name')->values());

        return Inertia::render('Modules/ModuleStore', [
            'modules' => $modules->map(fn ($module): array => [
                'keyName' => $module->key_name,
                'name' => $module->name_fa,
                'description' => $module->description ?: 'توضیحی برای این ماژول ثبت نشده است.',
                'enabled' => (bool) $module->enabled,
                'dependencies' => $dependencies->get($module->key_name, collect())->values(),
            ])->values(),
        ]);
    }
}
