<?php

namespace App\Support;

use App\Models\Setting;

/**
 * Editable "select option" registry. Ships sensible defaults for the dropdown
 * lists used across forms; admins override them from the Registry page and the
 * overrides are stored as one JSON blob in the settings table.
 */
class OptionRegistry
{
    private const STORE_KEY = 'option_registry';

    /** Default option lists, keyed by registry key. */
    public static function defaults(): array
    {
        return [
            'blood_groups' => ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-'],
            'provinces' => ['Kabul', 'Herat', 'Balkh', 'Kandahar', 'Nangarhar', 'Bamyan', 'Ghazni', 'Kunduz', 'Badakhshan', 'Takhar', 'Faryab', 'Helmand'],
            'student_statuses' => ['active', 'graduated', 'transferred', 'suspended', 'withdrawn'],
            'relationships' => ['Father', 'Mother', 'Grandfather', 'Grandmother', 'Uncle', 'Brother', 'Guardian'],
            'employment_types' => ['permanent', 'contract', 'part-time', 'substitute'],
            'staff_designations' => ['Teacher', 'Senior Teacher', 'Head of Department', 'Principal', 'Vice Principal', 'Admin Officer', 'Accountant', 'Librarian', 'Lab Assistant', 'Guard', 'Cleaner', 'Driver'],
            'departments' => ['Administration', 'Academics', 'Finance', 'Library', 'IT', 'Transport', 'Maintenance', 'Security'],
            'incident_categories' => ['Lateness', 'Misconduct', 'Bullying', 'Dress code', 'Academic dishonesty', 'Absenteeism', 'Policy breach', 'Property damage', 'Safety'],
            'incident_severities' => ['minor', 'moderate', 'severe'],
            'inventory_categories' => ['Stationery', 'Furniture', 'IT', 'Lab', 'Sports', 'Cleaning', 'Other'],
            'event_types' => ['event', 'holiday', 'exam', 'meeting'],
            'notice_audiences' => ['all', 'students', 'parents', 'staff'],
            'fee_heads' => ['Tuition', 'Admission', 'Transport', 'Exam', 'Library', 'Uniform', 'Stationery', 'Other'],
            'visitor_purposes' => ['Meet teacher', 'Fee inquiry', 'Admission enquiry', 'Parent–teacher meeting', 'Collect documents', 'Other'],
            'health_complaints' => ['Fever', 'Headache', 'Stomach ache', 'Injury', 'Nose bleed', 'Allergy', 'Dental', 'Other'],
        ];
    }

    /** UI metadata (icon + i18n label key) for each set. */
    public static function meta(): array
    {
        return [
            'blood_groups' => ['icon' => 'bloodtype'],
            'provinces' => ['icon' => 'map'],
            'student_statuses' => ['icon' => 'how_to_reg'],
            'relationships' => ['icon' => 'family_restroom'],
            'employment_types' => ['icon' => 'badge'],
            'staff_designations' => ['icon' => 'work'],
            'departments' => ['icon' => 'apartment'],
            'incident_categories' => ['icon' => 'report_problem'],
            'incident_severities' => ['icon' => 'priority_high'],
            'inventory_categories' => ['icon' => 'inventory_2'],
            'event_types' => ['icon' => 'event'],
            'notice_audiences' => ['icon' => 'campaign'],
            'fee_heads' => ['icon' => 'payments'],
            'visitor_purposes' => ['icon' => 'meeting_room'],
            'health_complaints' => ['icon' => 'medical_services'],
        ];
    }

    /** Effective sets: defaults overridden by saved values. */
    public static function all(): array
    {
        $defaults = self::defaults();
        $saved = Setting::get(self::STORE_KEY, []);
        $out = [];
        foreach ($defaults as $key => $items) {
            $out[$key] = array_values(array_key_exists($key, $saved) && is_array($saved[$key]) ? $saved[$key] : $items);
        }

        return $out;
    }

    /** A single list (effective). */
    public static function list(string $key): array
    {
        return self::all()[$key] ?? [];
    }

    /**
     * Persist overrides. Only known keys are accepted; each is sanitised to a
     * clean list of non-empty, de-duplicated string values.
     */
    public static function save(array $sets): array
    {
        $known = array_keys(self::defaults());
        $clean = [];
        foreach ($sets as $key => $items) {
            if (! in_array($key, $known, true) || ! is_array($items)) {
                continue;
            }
            $vals = [];
            foreach ($items as $v) {
                $v = trim((string) $v);
                if ($v !== '' && ! in_array($v, $vals, true)) {
                    $vals[] = $v;
                }
            }
            $clean[$key] = $vals;
        }
        Setting::put(self::STORE_KEY, $clean);

        return self::all();
    }
}
