<?php

namespace Database\Seeders;

use App\Models\AcademicSession;
use App\Models\Branch;
use App\Models\Family;
use App\Models\SchoolClass;
use App\Models\Section;
use App\Models\Student;
use Illuminate\Database\Seeder;

class FamilyStudentSeeder extends Seeder
{
    private array $fatherNames = ['Muhammad Aslam', 'Imran Khan', 'Tariq Mehmood', 'Asif Ali', 'Rashid Hussain', 'Naveed Akhtar', 'Kamran Shah', 'Zahid Iqbal', 'Faisal Mahmood', 'Saeed Ahmed'];
    private array $childNames = ['Ahmed', 'Ali', 'Fatima', 'Ayesha', 'Hassan', 'Hussain', 'Zainab', 'Maryam', 'Bilal', 'Usman', 'Sara', 'Hamza', 'Eman', 'Abdullah', 'Khadija'];

    public function run(): void
    {
        $session = AcademicSession::current();

        // Seed sample data into the two most-used campuses.
        foreach (Branch::whereIn('code', ['MAIN', 'GIRL'])->get() as $branch) {
            $classes = SchoolClass::where('branch_id', $branch->id)->with('sections')->get();
            if ($classes->isEmpty()) {
                continue;
            }

            for ($i = 0; $i < 8; $i++) {
                $father = $this->fatherNames[array_rand($this->fatherNames)];

                $family = Family::create([
                    'branch_id' => $branch->id,
                    'family_code' => Family::nextCode($branch->id),
                    'father_name' => $father,
                    'father_cnic' => $this->cnic(),
                    'father_mobile' => $this->mobile(),
                    'father_occupation' => collect(['Businessman', 'Engineer', 'Doctor', 'Teacher', 'Govt. Servant'])->random(),
                    'mother_name' => 'Mrs. '.$father,
                    'mother_mobile' => $this->mobile(),
                    'email' => 'family'.$branch->id.$i.'@example.test',
                    'address' => collect(['Model Town', 'Gulberg', 'DHA', 'Johar Town'])->random().', '.$branch->city,
                    'emergency_contact' => $this->mobile(),
                ]);

                // 1–3 children per family (demonstrates siblings + discounts later).
                $childCount = random_int(1, 3);
                for ($c = 0; $c < $childCount; $c++) {
                    $class = $classes->random();
                    $section = $class->sections->isNotEmpty() ? $class->sections->random() : null;
                    $gender = $branch->code === 'GIRL' ? 'female' : collect(['male', 'female'])->random();

                    Student::create([
                        'branch_id' => $branch->id,
                        'family_id' => $family->id,
                        'academic_session_id' => $session?->id,
                        'class_id' => $class->id,
                        'section_id' => $section?->id,
                        'admission_number' => Student::nextAdmissionNumber($branch->id),
                        'roll_number' => (string) random_int(1, 40),
                        'gr_number' => 'GR-'.random_int(1000, 9999),
                        'name' => $this->childNames[array_rand($this->childNames)].' '.$father,
                        'gender' => $gender,
                        'date_of_birth' => now()->subYears(random_int(6, 14))->subDays(random_int(0, 360))->toDateString(),
                        'religion' => 'Islam',
                        'nationality' => 'Pakistani',
                        'blood_group' => collect(['A+', 'B+', 'O+', 'AB+', 'O-'])->random(),
                        'admission_date' => now()->subMonths(random_int(1, 30))->toDateString(),
                        'status' => 'active',
                    ]);
                }
            }
        }
    }

    private function cnic(): string
    {
        return random_int(10000, 99999).'-'.random_int(1000000, 9999999).'-'.random_int(1, 9);
    }

    private function mobile(): string
    {
        return '03'.random_int(0, 4).'-'.random_int(1000000, 9999999);
    }
}
