<?php

namespace Database\Seeders;

use App\Models\ClassPack;
use App\Models\ClassSession;
use App\Models\ClassType;
use App\Models\Coach;
use App\Models\ContentBlock;
use App\Models\Faq;
use App\Models\GalleryImage;
use App\Models\GallerySection;
use App\Models\MembershipTier;
use App\Models\MembershipTierFeature;
use App\Models\ScheduleSetting;
use App\Models\SiteSetting;
use App\Models\Testimonial;
use Illuminate\Database\Seeder;

class HeadBangersSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedSiteSettings();
        $this->seedClassTypes();
        $coaches = $this->seedCoaches();
        $this->seedClassSessions($coaches);
        $this->seedMembership();
        $this->seedTestimonials();
        $this->seedGallery();
        $this->seedFaqs();
        $this->seedContentBlocks();
        ScheduleSetting::query()->create(['week_start_date' => '2025-06-02']);
    }

    private function seedSiteSettings(): void
    {
        $settings = [
            'phone' => '+971 4 456 7890',
            'email' => 'train@headbangers.ae',
            'address' => 'Gate Village, Building 4<br>DIFC, Dubai, UAE',
            'hours' => 'Mon&ndash;Fri &middot; 06:00&ndash;22:00<br>Sat&ndash;Sun &middot; 08:00&ndash;20:00',
            'footer_tagline' => "Dubai's house of boxing. Elite coaching, championship conditioning, and a room that hits back — in the heart of DIFC.",
        ];

        foreach ($settings as $key => $value) {
            SiteSetting::query()->updateOrCreate(['key' => $key], ['value' => $value]);
        }
    }

    private function seedClassTypes(): void
    {
        $types = [
            ['slug' => 'heavy', 'number' => '01', 'name' => 'Heavy Hitter', 'description' => 'Our signature round. Boxing technique fused with strength HIIT across the bag, the floor and the rig. Loud music, real combinations, a serious sweat — the class that turns first-timers into regulars.', 'duration_minutes' => 45, 'level_label' => 'All levels', 'intensity' => 4, 'sort_order' => 1],
            ['slug' => 'technique', 'number' => '02', 'name' => 'Technique', 'description' => 'The sweet science, slowed down. Stance, guard, footwork and the jab that starts everything. Small groups and big detail — this is where good habits are built and bad ones get quietly retired.', 'duration_minutes' => 50, 'level_label' => 'Foundations', 'intensity' => 2, 'sort_order' => 2],
            ['slug' => 'strength', 'number' => '03', 'name' => 'Strength & Conditioning', 'description' => 'Build the engine behind the punch. Olympic lifts, conditioning circuits and recovery science, programmed by our S&C team to make you stronger, faster and harder to put away in the later rounds.', 'duration_minutes' => 55, 'level_label' => 'All levels', 'intensity' => 4, 'sort_order' => 3],
            ['slug' => 'sparring', 'number' => '04', 'name' => 'Sparring', 'description' => 'Controlled, coached and respectful. Test the work under real pressure with partners matched to your level. Headgear on, ego off. Open by progression once your coach gives the nod.', 'duration_minutes' => 60, 'level_label' => 'Advanced', 'intensity' => 5, 'sort_order' => 4],
            ['slug' => 'recovery', 'number' => '05', 'name' => 'Recovery', 'description' => 'The work between the work. Mobility, breathwork and contrast therapy to keep you moving well and coming back. Low intensity, high return — the secret weapon of every member who lasts.', 'duration_minutes' => 40, 'level_label' => 'All levels', 'intensity' => 1, 'sort_order' => 5],
            ['slug' => 'private', 'number' => '06', 'name' => 'Private 1:1', 'description' => 'You and a head coach. Pads, a plan and total accountability, tuned entirely to your goals — whether that\'s your first fight or your first clean combination. The fastest line to your best.', 'duration_minutes' => 60, 'level_label' => 'By booking', 'intensity' => 3, 'sort_order' => 6],
        ];

        foreach ($types as $type) {
            ClassType::query()->updateOrCreate(['slug' => $type['slug']], $type);
        }
    }

  /** @return array<string, Coach> */
    private function seedCoaches(): array
    {
        $coaches = [
            ['slug' => 'khalid-mansour', 'name' => 'Khalid "Hammer" Mansour', 'role' => 'Head Coach · Ex-National Team', 'bio' => 'A two-time national champion who cornered title fights before founding Head Bangers. Khalid\'s sessions are technical, relentless and weirdly addictive. If you want to learn the craft properly, you start with him.', 'photo_path' => 'tex-a.jpg', 'media_tag' => 'Head Coach', 'tags' => ['Boxing IQ', 'Pads', 'Fight prep'], 'show_on_home' => true, 'show_on_about' => true, 'sort_order' => 1],
            ['slug' => 'lina-okafor', 'name' => 'Lina Okafor', 'role' => 'Strength & Conditioning Lead', 'bio' => 'MSc in sport science and a former international sprinter. Lina builds the engines behind our fighters — programming strength, power and recovery so you hit harder and last longer in every round.', 'photo_path' => 'tex-c.jpg', 'media_tag' => 'Performance', 'tags' => ['S&C', 'Power', 'Recovery'], 'show_on_home' => true, 'show_on_about' => true, 'sort_order' => 2],
            ['slug' => 'diego-santos', 'name' => 'Diego Santos', 'role' => 'Technique & Footwork', 'bio' => 'Trained in the Brazilian and Cuban traditions, Diego is obsessed with movement. His Technique classes are where members quietly fall in love with the sweet science — one perfect step at a time.', 'photo_path' => 'tex-d.jpg', 'media_tag' => 'Technique', 'tags' => ['Footwork', 'Defence', 'Beginners'], 'show_on_home' => true, 'show_on_about' => true, 'sort_order' => 3],
            ['slug' => 'yuki-tanaka', 'name' => 'Yuki Tanaka', 'role' => 'Sparring & Performance', 'bio' => 'A veteran of the amateur circuit, Yuki runs our sparring program with a fierce respect for safety. Calm, exacting and impossible to rattle — exactly who you want guiding your first rounds under pressure.', 'photo_path' => 'tex-e.jpg', 'media_tag' => null, 'tags' => ['Sparring', 'Conditioning', 'Mindset'], 'show_on_home' => false, 'show_on_about' => true, 'sort_order' => 4],
            ['slug' => 'amira-hassan', 'name' => 'Amira Hassan', 'role' => 'Recovery Lead', 'bio' => 'Mobility specialist and breathwork coach. Amira keeps members moving well between the hard rounds.', 'photo_path' => 'tex-b.jpg', 'media_tag' => null, 'tags' => ['Recovery', 'Mobility'], 'show_on_home' => false, 'show_on_about' => false, 'sort_order' => 5],
        ];

        $map = [];
        foreach ($coaches as $coach) {
            $map[$coach['slug']] = Coach::query()->updateOrCreate(['slug' => $coach['slug']], $coach);
        }

        return $map;
    }

    /** @param array<string, Coach> $coaches */
    private function seedClassSessions(array $coaches): void
    {
        ClassSession::query()->delete();

        $types = ClassType::query()->pluck('id', 'slug');
        $sessions = [
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'mon', 'time' => '06:15', 'duration' => 45, 'spots' => '3 left', 'availability' => 'low'],
            ['type' => 'technique', 'coach' => 'diego-santos', 'day' => 'mon', 'time' => '12:15', 'duration' => 50, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'mon', 'time' => '18:00', 'duration' => 45, 'spots' => 'Waitlist', 'availability' => 'waitlist'],
            ['type' => 'strength', 'coach' => 'lina-okafor', 'day' => 'mon', 'time' => '19:15', 'duration' => 55, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'strength', 'coach' => 'lina-okafor', 'day' => 'tue', 'time' => '07:30', 'duration' => 55, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'heavy', 'coach' => 'yuki-tanaka', 'day' => 'tue', 'time' => '12:15', 'duration' => 45, 'spots' => '5 left', 'availability' => 'low'],
            ['type' => 'sparring', 'coach' => 'yuki-tanaka', 'day' => 'tue', 'time' => '19:15', 'duration' => 60, 'spots' => '2 left', 'availability' => 'low'],
            ['type' => 'recovery', 'coach' => 'amira-hassan', 'day' => 'tue', 'time' => '20:30', 'duration' => 40, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'wed', 'time' => '06:15', 'duration' => 45, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'technique', 'coach' => 'diego-santos', 'day' => 'wed', 'time' => '12:15', 'duration' => 50, 'spots' => '4 left', 'availability' => 'low'],
            ['type' => 'heavy', 'coach' => 'yuki-tanaka', 'day' => 'wed', 'time' => '18:00', 'duration' => 45, 'spots' => 'Waitlist', 'availability' => 'waitlist'],
            ['type' => 'strength', 'coach' => 'lina-okafor', 'day' => 'wed', 'time' => '19:15', 'duration' => 55, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'technique', 'coach' => 'diego-santos', 'day' => 'thu', 'time' => '07:30', 'duration' => 50, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'thu', 'time' => '12:15', 'duration' => 45, 'spots' => '3 left', 'availability' => 'low'],
            ['type' => 'sparring', 'coach' => 'yuki-tanaka', 'day' => 'thu', 'time' => '19:15', 'duration' => 60, 'spots' => '1 left', 'availability' => 'low'],
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'fri', 'time' => '06:15', 'duration' => 45, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'strength', 'coach' => 'lina-okafor', 'day' => 'fri', 'time' => '12:15', 'duration' => 55, 'spots' => '6 left', 'availability' => 'low'],
            ['type' => 'heavy', 'coach' => 'yuki-tanaka', 'day' => 'fri', 'time' => '18:00', 'duration' => 45, 'spots' => '2 left', 'availability' => 'low'],
            ['type' => 'heavy', 'coach' => 'khalid-mansour', 'day' => 'sat', 'time' => '09:00', 'duration' => 45, 'spots' => 'Waitlist', 'availability' => 'waitlist'],
            ['type' => 'technique', 'coach' => 'diego-santos', 'day' => 'sat', 'time' => '10:30', 'duration' => 50, 'spots' => '4 left', 'availability' => 'low'],
            ['type' => 'recovery', 'coach' => 'amira-hassan', 'day' => 'sat', 'time' => '12:00', 'duration' => 40, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'strength', 'coach' => 'lina-okafor', 'day' => 'sun', 'time' => '09:00', 'duration' => 55, 'spots' => 'Open', 'availability' => 'open'],
            ['type' => 'recovery', 'coach' => 'amira-hassan', 'day' => 'sun', 'time' => '11:00', 'duration' => 40, 'spots' => '5 left', 'availability' => 'low'],
        ];

        foreach ($sessions as $session) {
            ClassSession::query()->create([
                'class_type_id' => $types[$session['type']],
                'coach_id' => $coaches[$session['coach']]->id,
                'day_of_week' => $session['day'],
                'start_time' => $session['time'],
                'duration_minutes' => $session['duration'],
                'spots_label' => $session['spots'],
                'availability' => $session['availability'],
            ]);
        }
    }

    private function seedMembership(): void
    {
        $contender = MembershipTier::query()->updateOrCreate(['slug' => 'contender'], [
            'name' => 'Contender', 'description' => 'For the building habit. Steady, structured, sustainable.',
            'price_cents' => 75000, 'billing_period' => 'month', 'cta_variant' => 'ghost', 'sort_order' => 1,
        ]);
        $champion = MembershipTier::query()->updateOrCreate(['slug' => 'champion'], [
            'name' => 'Champion', 'description' => 'For the committed. Unlimited rounds, full access, every day.',
            'price_cents' => 120000, 'billing_period' => 'month', 'is_featured' => true,
            'badge_text' => 'Most popular', 'cta_variant' => 'primary', 'sort_order' => 2,
        ]);
        $dynasty = MembershipTier::query()->updateOrCreate(['slug' => 'dynasty'], [
            'name' => 'Dynasty', 'description' => 'For the all-in. The complete club, billed annually.',
            'price_cents' => 1200000, 'billing_period' => 'year', 'cta_variant' => 'ghost', 'sort_order' => 3,
        ]);

        $features = [
            $contender->id => [
                ['8 group classes / month', true],
                ['All program types', true],
                ['Gloves & wraps provided', true],
                ['Recovery suite access', true],
                ['Guest passes', false],
                ['Private session credits', false],
            ],
            $champion->id => [
                ['Unlimited group classes', true],
                ['All program types', true],
                ['Gloves & wraps provided', true],
                ['Full recovery suite & sauna', true],
                ['2 guest passes / month', true],
                ['10% off private sessions', true],
            ],
            $dynasty->id => [
                ['Everything in Champion', true],
                ['2 months free vs monthly', true],
                ['4 private sessions / year', true],
                ['4 guest passes / month', true],
                ['Priority booking & events', true],
                ['Branded kit on arrival', true],
            ],
        ];

        foreach ($features as $tierId => $tierFeatures) {
            MembershipTierFeature::query()->where('membership_tier_id', $tierId)->delete();
            foreach ($tierFeatures as $i => [$text, $included]) {
                MembershipTierFeature::query()->create([
                    'membership_tier_id' => $tierId,
                    'text' => $text,
                    'is_included' => $included,
                    'sort_order' => $i + 1,
                ]);
            }
        }

        $packs = [
            ['name' => 'Drop-in', 'price_cents' => 12000, 'per_unit_label' => 'single class', 'sort_order' => 1],
            ['name' => '10-Class Pack', 'price_cents' => 100000, 'per_unit_label' => 'AED 100 / class', 'sort_order' => 2],
            ['name' => '20-Class Pack', 'price_cents' => 180000, 'per_unit_label' => 'AED 90 / class', 'sort_order' => 3],
        ];

        foreach ($packs as $pack) {
            ClassPack::query()->updateOrCreate(['name' => $pack['name']], $pack);
        }
    }

    private function seedTestimonials(): void
    {
        $items = [
            ['quote' => 'I walked in unable to skip for thirty seconds. Eight months later I sparred my first round. This place rebuilt my confidence, not just my cardio.', 'author_name' => 'Sara A.', 'author_role' => 'Member since 2024', 'avatar_path' => 'tex-f.jpg', 'sort_order' => 1],
            ['quote' => 'The coaching is on another level. They actually correct your form mid-round. I\'ve trained across three continents — this is the best room in Dubai.', 'author_name' => 'Marcus T.', 'author_role' => 'Founding member', 'avatar_path' => 'tex-b.jpg', 'sort_order' => 2],
            ['quote' => 'Booked a single class to try it. Cancelled my old gym the same week. The energy when that first bell rings is genuinely unmatched.', 'author_name' => 'Priya K.', 'author_role' => 'Heavy Hitter regular', 'avatar_path' => 'tex-e.jpg', 'sort_order' => 3],
        ];

        foreach ($items as $item) {
            Testimonial::query()->updateOrCreate(['author_name' => $item['author_name']], $item);
        }
    }

    private function seedGallery(): void
    {
        GalleryImage::query()->where('is_featured', true)->delete();

        GalleryImage::query()->updateOrCreate(
            ['caption' => 'Fight night · DIFC'],
            ['image_path' => 'hero.jpg', 'is_featured' => true, 'height_class' => 'feature-tile', 'sort_order' => 0]
        );

        $sections = [
            ['slug' => 'the-floor', 'name' => 'The Floor', 'subtitle' => 'Rings · bags · iron', 'sort_order' => 1, 'images' => [
                ['image_path' => 'tex-a.jpg', 'caption' => 'The main ring', 'height_class' => 'h-md', 'grid_class' => 'g-2', 'sort_order' => 1],
                ['image_path' => 'tex-f.jpg', 'caption' => 'Heavy bag row', 'height_class' => 'h-md', 'grid_class' => 'g-2', 'sort_order' => 2],
                ['image_path' => 'tex-d.jpg', 'caption' => 'Strength floor', 'height_class' => 'h-sm', 'grid_class' => 'g-3', 'sort_order' => 3],
                ['image_path' => 'tex-c.jpg', 'caption' => 'Recovery suite', 'height_class' => 'h-sm', 'grid_class' => 'g-3', 'sort_order' => 4],
                ['image_path' => 'tex-e.jpg', 'caption' => 'Wraps & gloves', 'height_class' => 'h-sm', 'grid_class' => 'g-3', 'sort_order' => 5],
            ]],
            ['slug' => 'in-action', 'name' => 'In Action', 'subtitle' => 'Rounds in motion', 'sort_order' => 2, 'images' => [
                ['image_path' => 'tex-c.jpg', 'caption' => 'Pad work', 'height_class' => 'h-lg', 'grid_class' => 'g-asym', 'sort_order' => 1],
                ['image_path' => 'tex-b.jpg', 'caption' => 'Heavy Hitter, 18:00', 'height_class' => 'h-lg', 'grid_class' => 'g-asym', 'sort_order' => 2],
                ['image_path' => 'tex-a.jpg', 'caption' => 'Sparring night', 'height_class' => 'h-lg', 'grid_class' => 'g-asym', 'sort_order' => 3],
            ]],
            ['slug' => 'the-community', 'name' => 'The Community', 'subtitle' => '600+ strong', 'sort_order' => 3, 'images' => [
                ['image_path' => 'tex-e.jpg', 'caption' => 'First class', 'height_class' => 'h-sm', 'grid_class' => 'g-4', 'sort_order' => 1],
                ['image_path' => 'tex-d.jpg', 'caption' => 'The corner', 'height_class' => 'h-sm', 'grid_class' => 'g-4', 'sort_order' => 2],
                ['image_path' => 'tex-f.jpg', 'caption' => 'Post-round', 'height_class' => 'h-sm', 'grid_class' => 'g-4', 'sort_order' => 3],
                ['image_path' => 'tex-a.jpg', 'caption' => 'Members\' night', 'height_class' => 'h-sm', 'grid_class' => 'g-4', 'sort_order' => 4],
            ]],
        ];

        foreach ($sections as $sectionData) {
            $images = $sectionData['images'];
            unset($sectionData['images']);
            $section = GallerySection::query()->updateOrCreate(['slug' => $sectionData['slug']], $sectionData);
            foreach ($images as $image) {
                GalleryImage::query()->updateOrCreate(
                    ['gallery_section_id' => $section->id, 'caption' => $image['caption']],
                    array_merge($image, ['gallery_section_id' => $section->id])
                );
            }
        }
    }

    private function seedFaqs(): void
    {
        $faqs = [
            ['question' => 'I\'ve never boxed before. Is this for me?', 'answer' => 'Absolutely — most of our members started exactly there. Begin with Technique or Heavy Hitter; coaches scale every round to your level and there\'s zero pressure to spar. Your first class is free, so come find out.', 'sort_order' => 1],
            ['question' => 'What should I bring to my first class?', 'answer' => 'Just training clothes, a water bottle and yourself. We provide gloves and wraps for your first visit, and showers, towels and toiletries are on the house. Many members buy their own gloves later — we\'ll help you choose.', 'sort_order' => 2],
            ['question' => 'Can I freeze or cancel my membership?', 'answer' => 'Yes. Monthly memberships can be frozen for up to 3 months a year, or cancelled with 30 days\' notice — no penalties, no awkward phone calls. Everything is managed in the Head Bangers app.', 'sort_order' => 3],
            ['question' => 'How does booking work?', 'answer' => 'Reserve classes up to 7 days ahead in the app. Classes are capped to protect coaching quality, so booking is recommended — but if a class is full, join the waitlist and you\'ll be auto-enrolled when a spot opens.', 'sort_order' => 4],
            ['question' => 'Do you offer corporate or group rates?', 'answer' => 'We do. Head Bangers runs corporate memberships and private group sessions for teams across DIFC. Get in touch via the contact page and we\'ll build something for your crew.', 'sort_order' => 5],
        ];

        foreach ($faqs as $faq) {
            Faq::query()->updateOrCreate(['question' => $faq['question']], array_merge($faq, ['context' => 'membership']));
        }
    }

    private function seedContentBlocks(): void
    {
        $blocks = [
            ['page' => 'home', 'section_key' => 'hero', 'title' => "DUBAI'S\nBOXING", 'body' => 'Elite coaching. Championship conditioning. A room that hits back — in the heart of DIFC. Whether it\'s your first wrap or your thousandth round, you belong on the canvas.', 'meta' => ['chip' => 'Now taking founding members', 'image' => 'hero.jpg', 'rating' => '4.9 / 5 · 600+ members', 'coaches_count' => '12', 'classes_count' => '40+']],
            ['page' => 'home', 'section_key' => 'intro', 'title' => 'Not a gym. A fight camp built for the city that never settles.', 'body' => 'Head Bangers brings the discipline of a professional fight camp to Dubai — minus the ego. Group rounds that move like a class and burn like a session. Coaches who\'ve cornered title fights. And a community that shows up for each other, every single bell.'],
            ['page' => 'home', 'section_key' => 'cta', 'title' => "Hear the bell.\nAnswer it.", 'meta' => ['kicker' => 'Ready when you are', 'image' => 'action.jpg']],
            ['page' => 'about', 'section_key' => 'hero', 'title' => "We don't sell\nfitness. We\ncoach fighters.", 'body' => 'Head Bangers was founded on one belief — that anyone, at any level, deserves to be coached like they matter. We brought the discipline of a professional fight camp to Dubai and stripped away the ego. What\'s left is the purest version of the sport: hard work, real technique, and a room that has your back.', 'meta' => ['image' => 'tex-a.jpg', 'media_tag' => 'The floor · DIFC']],
            ['page' => 'about', 'section_key' => 'philosophy', 'title' => "The bell\ndoesn't care\nwho you are.", 'body' => 'When the round starts, titles disappear. The CEO and the student sweat the same. That equaliser is why we fell in love with boxing — and why we built a room where it\'s protected.'],
            ['page' => 'about', 'section_key' => 'cta', 'title' => "Meet us on\nthe canvas.", 'meta' => ['kicker' => 'Come see for yourself', 'image' => 'action.jpg']],
            ['page' => 'classes', 'section_key' => 'hero', 'title' => "Pick your\nround.", 'body' => 'Six coach-led programs, forty-plus classes a week, every one capped so you actually get coached. First class is free — no experience required, just show up.'],
            ['page' => 'membership', 'section_key' => 'hero', 'title' => "Train your\nway.", 'body' => 'No lock-ins, no fine print. Pick a membership for the full experience, or grab a class pack and come when you can. Every option includes gloves, wraps and full facility access.'],
            ['page' => 'membership', 'section_key' => 'intro_offer', 'title' => 'Your first class is free.', 'body' => 'Try any group round on the house — no card, no commitment. Love it? New members get 50% off their first month when they join within 7 days.'],
            ['page' => 'membership', 'section_key' => 'cta', 'title' => "Lock in your\nspot.", 'meta' => ['kicker' => "Founding rates won't last", 'image' => 'action.jpg']],
            ['page' => 'gallery', 'section_key' => 'hero', 'title' => "Inside the\nhouse.", 'body' => 'The floor, the rings, the rounds and the people who fill them.'],
            ['page' => 'gallery', 'section_key' => 'cta', 'title' => "Photos don't\nsweat. You will.", 'meta' => ['kicker' => 'See it for real', 'image' => 'action.jpg']],
            ['page' => 'contact', 'section_key' => 'hero', 'title' => "Step into\nthe gym.", 'body' => 'Questions about classes, memberships or corporate sessions? Drop us a line, or just turn up — the kettle\'s on and the bags are ready.'],
        ];

        foreach ($blocks as $block) {
            ContentBlock::query()->updateOrCreate(
                ['page' => $block['page'], 'section_key' => $block['section_key']],
                $block
            );
        }
    }
}
