<?php

namespace Database\Seeders;

use App\Models\Gallery;
use App\Models\GalleryImage;
use App\Models\GalleryType;
use App\Models\Organization;
use Illuminate\Database\Seeder;

class GallerySeeder extends Seeder
{
    public function run(): void
    {
        $organization = Organization::first();
        if (!$organization) {
            $this->call(OrganizationSeeder::class);
            $organization = Organization::first();
        }

        $this->call(GalleryTypeSeeder::class);

        $trainingType = GalleryType::where('name', 'Training Sessions')->first();
        $graduationType = GalleryType::where('name', 'Graduations')->first();
        $eventsType = GalleryType::where('name', 'Events')->first();
        $teamType = GalleryType::where('name', 'Team')->first();
        $facilitiesType = GalleryType::where('name', 'Facilities')->first();

        // Training Sessions Gallery
        $trainingGallery = Gallery::create([
            'organization_id' => $organization->id,
            'gallery_type_id' => $trainingType->id,
            'title' => 'Mediation Training Workshop 2025',
            'description' => 'Intensive five-day professional mediation training workshop',
            'is_published' => true,
            'sort_order' => 1,
        ]);

        $trainingImages = [
            ['title' => 'Opening Session', 'image_path' => 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=800&h=600&fit=crop'],
            ['title' => 'Role-Play Exercise', 'image_path' => 'https://images.unsplash.com/photo-1573497019940-1c28c88b4f3e?w=800&h=600&fit=crop'],
            ['title' => 'Group Discussion', 'image_path' => 'https://images.unsplash.com/photo-1521737711867-e3b97375f902?w=800&h=600&fit=crop'],
            ['title' => 'Skills Practice', 'image_path' => 'https://images.unsplash.com/photo-1600880292203-757bb62b4baf?w=800&h=600&fit=crop'],
            ['title' => 'Interactive Session', 'image_path' => 'https://images.unsplash.com/photo-1524178232363-1fb2b075b655?w=800&h=600&fit=crop'],
            ['title' => 'Mediation Demo', 'image_path' => 'https://images.unsplash.com/photo-1517245386807-bb43f82c33c4?w=800&h=600&fit=crop'],
        ];

        foreach ($trainingImages as $i => $img) {
            GalleryImage::create([
                'gallery_id' => $trainingGallery->id,
                'title' => $img['title'],
                'image_path' => $img['image_path'],
                'image_name' => $img['title'],
                'sort_order' => $i,
            ]);
        }
        $trainingGallery->syncImageCount();

        // Graduation Gallery
        $gradGallery = Gallery::create([
            'organization_id' => $organization->id,
            'gallery_type_id' => $graduationType->id,
            'title' => 'Certification Ceremony 2025',
            'description' => 'Celebrating our latest cohort of certified mediators',
            'is_published' => true,
            'sort_order' => 2,
        ]);

        $gradImages = [
            ['title' => 'Certificate Presentation', 'image_path' => 'https://images.unsplash.com/photo-1523050854058-8df90110c476?w=800&h=600&fit=crop'],
            ['title' => 'Graduates Group Photo', 'image_path' => 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=800&h=600&fit=crop'],
            ['title' => 'Celebration Moment', 'image_path' => 'https://images.unsplash.com/photo-1511578314322-379afb476865?w=800&h=600&fit=crop'],
            ['title' => 'Keynote Address', 'image_path' => 'https://images.unsplash.com/photo-1475721027785-f74eccf877e2?w=800&h=600&fit=crop'],
        ];

        foreach ($gradImages as $i => $img) {
            GalleryImage::create([
                'gallery_id' => $gradGallery->id,
                'title' => $img['title'],
                'image_path' => $img['image_path'],
                'image_name' => $img['title'],
                'sort_order' => $i,
            ]);
        }
        $gradGallery->syncImageCount();

        // Events Gallery
        $eventsGallery = Gallery::create([
            'organization_id' => $organization->id,
            'gallery_type_id' => $eventsType->id,
            'title' => 'ADR Conference 2025',
            'description' => 'Annual Alternative Dispute Resolution conference highlights',
            'is_published' => true,
            'sort_order' => 3,
        ]);

        $eventImages = [
            ['title' => 'Conference Hall', 'image_path' => 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=800&h=600&fit=crop'],
            ['title' => 'Panel Discussion', 'image_path' => 'https://images.unsplash.com/photo-1517245386807-bb43f82c33c4?w=800&h=600&fit=crop'],
            ['title' => 'Networking Session', 'image_path' => 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=800&h=600&fit=crop'],
        ];

        foreach ($eventImages as $i => $img) {
            GalleryImage::create([
                'gallery_id' => $eventsGallery->id,
                'title' => $img['title'],
                'image_path' => $img['image_path'],
                'image_name' => $img['title'],
                'sort_order' => $i,
            ]);
        }
        $eventsGallery->syncImageCount();
    }
}
