<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\HealthInstitution;
use App\Models\User;
use Illuminate\Support\Str;

/**
 * HealthInstitutionSeeder
 *
 * Seeds 10 realistic South Sudan health institution listings.
 * All records are fully published with paid payment status.
 *
 * Depends on: UserSeeder (Health Marketor user must exist)
 */
class HealthInstitutionSeeder extends Seeder
{
    public function run(): void
    {
        $marketor = User::where('email', 'healthmarketor@sshia.org')->first();
        $userId = $marketor?->id;

        $institutions = [
            [
                'name'           => 'Juba Teaching Hospital',
                'short_name'     => 'JTH',
                'city'           => 'Juba',
                'address'        => 'Juba Teaching Hospital Road, Juba',
                'country'        => 'South Sudan',
                'latitude'       => 4.8594,
                'longitude'      => 31.5713,
                'email'          => 'info@jubateachinghospital.ss',
                'phone'          => '+211 920 000 001',
                'website_url'    => 'https://www.jubateachinghospital.ss',
                'description'    => 'Juba Teaching Hospital is the largest referral hospital in South Sudan, providing comprehensive medical services including emergency care, surgery, pediatrics, and specialist consultations to the population of Juba and surrounding regions.',
                'services'       => ['General Medicine', 'Surgery', 'Pediatrics', 'Obstetrics & Gynecology', 'Emergency Services', 'Radiology', 'Laboratory Services', 'Pharmacy'],
                'images'         => ['institutions/jth-main.jpg', 'institutions/jth-ward.jpg', 'institutions/jth-lab.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 342,
                'published_at'   => now()->subMonths(6),
                'expires_at'     => now()->addMonths(6),
                'next_payment_date' => now()->addMonths(6),
            ],
            [
                'name'           => 'Wau General Hospital',
                'short_name'     => 'WGH',
                'city'           => 'Wau',
                'address'        => 'Hospital Road, Wau, Western Bahr el Ghazal',
                'country'        => 'South Sudan',
                'latitude'       => 7.7010,
                'longitude'      => 27.9947,
                'email'          => 'contact@waugeneralhospital.ss',
                'phone'          => '+211 920 000 002',
                'website_url'    => null,
                'description'    => 'Wau General Hospital serves as the primary referral facility for Western Bahr el Ghazal State, offering inpatient and outpatient services, maternity care, and HIV/AIDS treatment programs.',
                'services'       => ['General Medicine', 'Maternity', 'HIV/AIDS Care', 'Vaccination', 'Laboratory Services', 'Pharmacy', 'Emergency Services'],
                'images'         => ['institutions/wgh-front.jpg', 'institutions/wgh-maternity.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 198,
                'published_at'   => now()->subMonths(5),
                'expires_at'     => now()->addMonths(7),
                'next_payment_date' => now()->addMonths(7),
            ],
            [
                'name'           => 'Malakal Teaching Hospital',
                'short_name'     => 'MTH',
                'city'           => 'Malakal',
                'address'        => 'Malakal Teaching Hospital, Upper Nile State',
                'country'        => 'South Sudan',
                'latitude'       => 9.5334,
                'longitude'      => 31.6605,
                'email'          => 'mth@uppernilemoh.ss',
                'phone'          => '+211 920 000 003',
                'website_url'    => null,
                'description'    => 'Malakal Teaching Hospital is the main referral hospital for Upper Nile State, providing surgical, medical, and maternal health services. It also serves as a training facility for medical students and nurses.',
                'services'       => ['General Medicine', 'Surgery', 'Obstetrics & Gynecology', 'Pediatrics', 'Mental Health', 'Laboratory Services', 'Radiology'],
                'images'         => ['institutions/mth-main.jpg', 'institutions/mth-surgery.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 156,
                'published_at'   => now()->subMonths(4),
                'expires_at'     => now()->addMonths(8),
                'next_payment_date' => now()->addMonths(8),
            ],
            [
                'name'           => 'Nimule General Hospital',
                'short_name'     => 'NGH',
                'city'           => 'Nimule',
                'address'        => 'Nimule Town, Eastern Equatoria State',
                'country'        => 'South Sudan',
                'latitude'       => 3.5997,
                'longitude'      => 32.0583,
                'email'          => 'nimulehospital@easternequatoria.ss',
                'phone'          => '+211 920 000 004',
                'website_url'    => 'https://nimulehospital.ss',
                'description'    => 'Nimule General Hospital is a key health facility at the Uganda-South Sudan border, providing essential health services to the local population and cross-border communities. It specializes in tropical disease management and emergency care.',
                'services'       => ['General Medicine', 'Emergency Services', 'Vaccination', 'HIV/AIDS Care', 'Pharmacy', 'Health Checkups', 'Laboratory Services'],
                'images'         => ['institutions/ngh-entrance.jpg', 'institutions/ngh-pharmacy.jpg', 'institutions/ngh-ward.jpg'],
                'videos'         => ['https://www.youtube.com/watch?v=example1'],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 211,
                'published_at'   => now()->subMonths(3),
                'expires_at'     => now()->addMonths(9),
                'next_payment_date' => now()->addMonths(9),
            ],
            [
                'name'           => 'Yei Civil Hospital',
                'short_name'     => 'YCH',
                'city'           => 'Yei',
                'address'        => 'Yei Town, Central Equatoria State',
                'country'        => 'South Sudan',
                'latitude'       => 4.0944,
                'longitude'      => 30.6778,
                'email'          => 'yeicivilhospital@centralequatoria.ss',
                'phone'          => '+211 920 000 005',
                'website_url'    => null,
                'description'    => 'Yei Civil Hospital provides comprehensive healthcare services to the residents of Yei River County and surrounding areas. The hospital offers maternal health, pediatric care, and general outpatient services.',
                'services'       => ['General Medicine', 'Maternity', 'Pediatrics', 'Dental Care', 'Vaccination', 'Laboratory Services', 'Pharmacy'],
                'images'         => ['institutions/ych-main.jpg', 'institutions/ych-pediatrics.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 134,
                'published_at'   => now()->subMonths(7),
                'expires_at'     => now()->addMonths(5),
                'next_payment_date' => now()->addMonths(5),
            ],
            [
                'name'           => 'Rumbek State Hospital',
                'short_name'     => 'RSH',
                'city'           => 'Rumbek',
                'address'        => 'Rumbek Town, Lakes State',
                'country'        => 'South Sudan',
                'latitude'       => 6.8083,
                'longitude'      => 29.6794,
                'email'          => 'rumbek.hospital@lakesstate.ss',
                'phone'          => '+211 920 000 006',
                'website_url'    => null,
                'description'    => 'Rumbek State Hospital is the main referral hospital for Lakes State, offering a wide range of medical services including surgery, obstetrics, and specialist consultations. It plays a critical role in the health system of the greater Lakes region.',
                'services'       => ['General Medicine', 'Surgery', 'Obstetrics & Gynecology', 'Physiotherapy', 'Laboratory Services', 'Radiology', 'Emergency Services', 'Pharmacy'],
                'images'         => ['institutions/rsh-front.jpg', 'institutions/rsh-lab.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 178,
                'published_at'   => now()->subMonths(8),
                'expires_at'     => now()->addMonths(4),
                'next_payment_date' => now()->addMonths(4),
            ],
            [
                'name'           => 'Torit General Hospital',
                'short_name'     => 'TGH',
                'city'           => 'Torit',
                'address'        => 'Torit Town, Eastern Equatoria State',
                'country'        => 'South Sudan',
                'latitude'       => 4.4100,
                'longitude'      => 32.5700,
                'email'          => 'toritgeneralhospital@easternequatoria.ss',
                'phone'          => '+211 920 000 007',
                'website_url'    => null,
                'description'    => 'Torit General Hospital serves Eastern Equatoria State with essential health services. The hospital focuses on maternal and child health, tropical disease management, and community health outreach programs.',
                'services'       => ['General Medicine', 'Maternity', 'Pediatrics', 'HIV/AIDS Care', 'Vaccination', 'Health Checkups', 'Laboratory Services'],
                'images'         => ['institutions/tgh-main.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 89,
                'published_at'   => now()->subMonths(2),
                'expires_at'     => now()->addMonths(10),
                'next_payment_date' => now()->addMonths(10),
            ],
            [
                'name'           => 'Aweil Civil Hospital',
                'short_name'     => 'ACH',
                'city'           => 'Aweil',
                'address'        => 'Aweil Town, Northern Bahr el Ghazal State',
                'country'        => 'South Sudan',
                'latitude'       => 8.7676,
                'longitude'      => 27.3969,
                'email'          => 'aweilhospital@northernbeg.ss',
                'phone'          => '+211 920 000 008',
                'website_url'    => null,
                'description'    => 'Aweil Civil Hospital is the primary health facility in Northern Bahr el Ghazal State, providing inpatient, outpatient, and emergency services. The hospital has a dedicated nutrition unit addressing malnutrition in the region.',
                'services'       => ['General Medicine', 'Emergency Services', 'Pediatrics', 'Maternity', 'Vaccination', 'Laboratory Services', 'Pharmacy', 'Health Checkups'],
                'images'         => ['institutions/ach-entrance.jpg', 'institutions/ach-nutrition.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 143,
                'published_at'   => now()->subMonths(9),
                'expires_at'     => now()->addMonths(3),
                'next_payment_date' => now()->addMonths(3),
            ],
            [
                'name'           => 'Bor State Hospital',
                'short_name'     => 'BSH',
                'city'           => 'Bor',
                'address'        => 'Bor Town, Jonglei State',
                'country'        => 'South Sudan',
                'latitude'       => 6.2108,
                'longitude'      => 31.5591,
                'email'          => 'borstatehospital@jonglei.ss',
                'phone'          => '+211 920 000 009',
                'website_url'    => 'https://borstatehospital.ss',
                'description'    => 'Bor State Hospital is the main referral hospital for Jonglei State, offering comprehensive medical services. The hospital has recently expanded its surgical and maternity units to serve the growing population of the region.',
                'services'       => ['General Medicine', 'Surgery', 'Maternity', 'Pediatrics', 'Eye Care', 'Laboratory Services', 'Radiology', 'Emergency Services', 'Pharmacy'],
                'images'         => ['institutions/bsh-main.jpg', 'institutions/bsh-surgery.jpg', 'institutions/bsh-maternity.jpg'],
                'videos'         => ['https://www.youtube.com/watch?v=example2'],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 267,
                'published_at'   => now()->subMonths(10),
                'expires_at'     => now()->addMonths(2),
                'next_payment_date' => now()->addMonths(2),
            ],
            [
                'name'           => 'Maridi District Hospital',
                'short_name'     => 'MDH',
                'city'           => 'Maridi',
                'address'        => 'Maridi Town, Western Equatoria State',
                'country'        => 'South Sudan',
                'latitude'       => 4.9167,
                'longitude'      => 29.4833,
                'email'          => 'maridihospital@westernequatoria.ss',
                'phone'          => '+211 920 000 010',
                'website_url'    => null,
                'description'    => 'Maridi District Hospital provides essential health services to Western Equatoria State. Known for its tropical disease management programs, the hospital also offers mental health services and community outreach initiatives.',
                'services'       => ['General Medicine', 'Mental Health', 'HIV/AIDS Care', 'Vaccination', 'Physiotherapy', 'Laboratory Services', 'Pharmacy', 'Specialist Consultations'],
                'images'         => ['institutions/mdh-front.jpg', 'institutions/mdh-mental-health.jpg'],
                'videos'         => [],
                'listing_status' => 'published',
                'payment_status' => 'paid',
                'is_published'   => true,
                'view_count'     => 112,
                'published_at'   => now()->subMonths(1),
                'expires_at'     => now()->addMonths(11),
                'next_payment_date' => now()->addMonths(11),
            ],
        ];

        foreach ($institutions as $data) {
            $name = $data['name'];

            // Idempotency: skip if already exists
            if (HealthInstitution::where('name', $name)->exists()) {
                $this->command->info("Skipping existing institution: {$name}");
                continue;
            }

            HealthInstitution::create(array_merge($data, [
                'user_id' => $userId,
                'slug'    => Str::slug($name),
            ]));

            $this->command->info("Created institution: {$name}");
        }

        $this->command->info('HealthInstitutionSeeder completed — 10 institutions seeded.');
    }
}
