<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\MediaCategory;
use App\Models\MediaItem;
use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Seeder;

class GalleryDocumentsDemoSeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::role('admin')->first();
        $user = User::role('user')->first();
        $mainProject = Project::where('status', 'active')->orderBy('id')->first();

        if (! $admin || ! $mainProject) {
            return;
        }

        $galleryParent = MediaCategory::firstOrCreate(
            ['name' => 'گالری دامداری', 'type' => MediaCategory::TYPE_GALLERY],
            ['sort_order' => 1]
        );

        $gallerySub = MediaCategory::firstOrCreate(
            ['name' => 'سالن نگهداری', 'parent_id' => $galleryParent->id, 'type' => MediaCategory::TYPE_GALLERY],
            ['sort_order' => 1]
        );

        $docPermits = MediaCategory::firstOrCreate(
            ['name' => 'مجوزها و گواهی‌نامه‌ها', 'type' => MediaCategory::TYPE_DOCUMENT],
            ['sort_order' => 1]
        );

        $docContracts = MediaCategory::firstOrCreate(
            ['name' => 'قراردادها و بیمه', 'type' => MediaCategory::TYPE_DOCUMENT],
            ['sort_order' => 2]
        );

        $galleryItems = [
            [
                'name' => 'نمای کلی سالن شیری',
                'type' => MediaItem::TYPE_IMAGE,
                'file_path' => 'files/gav.jpg',
                'category_id' => $galleryParent->id,
                'subcategory_id' => $gallerySub->id,
                'uploader_type' => MediaItem::UPLOADER_PRODUCER,
                'description' => 'تصویر نمای کلی سالن نگهداری گاوهای شیری',
                'tags' => ['سالن', 'گاوداری'],
                'gps_lat' => 35.8400,
                'gps_lng' => 50.9391,
            ],
            [
                'name' => 'تغذیه گله - وعده صبح',
                'type' => MediaItem::TYPE_IMAGE,
                'file_path' => 'files/gavo-goosfand-baham.jpg',
                'category_id' => $galleryParent->id,
                'subcategory_id' => $gallerySub->id,
                'uploader_type' => MediaItem::UPLOADER_ADMIN,
                'description' => 'ثبت وضعیت تغذیه صبحگاهی',
                'tags' => ['جیره', 'تغذیه'],
                'gps_lat' => 35.8412,
                'gps_lng' => 50.9405,
            ],
            [
                'name' => 'گله گوسفند در مرتع',
                'type' => MediaItem::TYPE_IMAGE,
                'file_path' => 'files/goosfand-2.jpg',
                'category_id' => $galleryParent->id,
                'subcategory_id' => $gallerySub->id,
                'uploader_type' => MediaItem::UPLOADER_PRODUCER,
                'description' => 'تصویر گله گوسفند در مرتع',
                'tags' => ['گوسفند', 'مرتع'],
                'gps_lat' => 35.8420,
                'gps_lng' => 50.9410,
            ],
            [
                'name' => 'بازدید میدانی دامداری',
                'type' => MediaItem::TYPE_IMAGE,
                'file_path' => 'files/gav-5.jpg',
                'category_id' => $galleryParent->id,
                'uploader_type' => MediaItem::UPLOADER_EMPLOYER,
                'description' => 'تصویر بازدید کارفرما از واحد دامداری',
                'tags' => ['بازدید'],
            ],
            [
                'name' => 'شتر واحد پرورشی',
                'type' => MediaItem::TYPE_IMAGE,
                'file_path' => 'files/shotor-1.jpg',
                'category_id' => $galleryParent->id,
                'uploader_type' => MediaItem::UPLOADER_PRODUCER,
                'description' => 'تصویر شتر در واحد پرورشی',
                'tags' => ['شتر'],
            ],
        ];

        foreach ($galleryItems as $item) {
            MediaItem::updateOrCreate(
                [
                    'project_id' => $mainProject->id,
                    'name' => $item['name'],
                ],
                array_merge($item, [
                    'uploader_id' => $user?->id,
                    'status' => MediaItem::STATUS_APPROVED,
                    'created_by' => $admin->id,
                    'created_at_file' => now()->subDays(rand(3, 20)),
                ])
            );
        }

        $documents = [
            [
                'name' => 'پروانه بهره‌برداری دامداری',
                'type' => MediaItem::TYPE_DOCUMENT,
                'file_path' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
                'category_id' => $docPermits->id,
                'contract_number' => 'PRM-1403-4521',
                'company_name' => 'سازمان جهاد کشاورزی البرز',
                'contact_phone' => '026-12345678',
                'start_at' => now()->subYear(),
                'end_at' => now()->addYear(),
            ],
            [
                'name' => 'بیمه‌نامه مسئولیت دامداری',
                'type' => MediaItem::TYPE_DOCUMENT,
                'file_path' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
                'category_id' => $docContracts->id,
                'contract_number' => 'INS-982134',
                'company_name' => 'بیمه ایران',
                'contact_phone' => '021-88776655',
                'start_at' => now()->subMonths(6),
                'end_at' => now()->addMonths(6),
            ],
            [
                'name' => 'قرارداد خدمات دامپزشکی',
                'type' => MediaItem::TYPE_DOCUMENT,
                'file_path' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
                'category_id' => $docContracts->id,
                'contract_number' => 'VET-2026-08',
                'company_name' => 'کلینیک دامپزشکی ریوو',
                'contact_phone' => '09121234567',
                'start_at' => now()->subMonths(2),
                'end_at' => now()->addMonths(10),
            ],
        ];

        foreach ($documents as $doc) {
            MediaItem::firstOrCreate(
                [
                    'project_id' => $mainProject->id,
                    'name' => $doc['name'],
                ],
                array_merge($doc, [
                    'uploader_type' => MediaItem::UPLOADER_ADMIN,
                    'uploader_id' => $admin->id,
                    'status' => MediaItem::STATUS_APPROVED,
                    'created_by' => $admin->id,
                    'created_at_file' => now()->subMonths(3),
                ])
            );
        }

        $secondProject = Project::where('status', 'active')->orderByDesc('id')->first();
        if ($secondProject && $secondProject->id !== $mainProject->id) {
            MediaItem::updateOrCreate(
                [
                    'project_id' => $secondProject->id,
                    'name' => 'آمار وزن‌کشی گله',
                ],
                [
                    'type' => MediaItem::TYPE_IMAGE,
                    'file_path' => 'files/goosfand-3.jpg',
                    'category_id' => $galleryParent->id,
                    'uploader_type' => MediaItem::UPLOADER_PRODUCER,
                    'uploader_id' => $user?->id,
                    'status' => MediaItem::STATUS_APPROVED,
                    'created_by' => $admin->id,
                ]
            );
        }
    }
}
