<?php

namespace Modules\Ad\database\seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
use Modules\Ad\Models\CustomAdsSetting;

class CustomAdsSettingTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Schema::disableForeignKeyConstraints();
        $customads = [
            [
                'id' => 1,
                'name' => 'PromoStream',
                'type' => 'video',
                'url_type' => 'url',
                'placement' => 'home_page',
                'media' => 'https://youtu.be/QVMOq6b7H6Y?si=tO3WX3zUIze96MTb',
                'redirect_url' => 'https://youtu.be/QVMOq6b7H6Y?si=tO3WX3zUIze96MTb',
                'duration' => '00:15',
                'skip_enabled' => 1,
                'skip_after' => '00:10',
                'target_content_type' => 'video',
                'target_categories' => json_encode([5,9,11]),
                'max_views' => 20,
                'status' => 1,
                'start_date' => '2025-06-20',
                'end_date' => '2025-06-30',
            ],
            [
                'id' => 2,
                'name' => 'AdWave Hub',
                'type' => 'video',
                'url_type' => 'url',
                'placement' => 'player',
                'media' => 'https://youtu.be/y1ZXkDDvhC4?si=MDRzE3VkpD9HJSKT',
                'redirect_url' => 'https://youtu.be/y1ZXkDDvhC4?si=MDRzE3VkpD9HJSKT',
                'duration' => '00:30',
                'skip_enabled' => 1,
                'skip_after' => '00:15',
                'target_content_type' => 'tvshow',
                'target_categories' => json_encode([4,10,11]),
                'max_views' => 50,
                'status' => 1,
                'start_date' => '2025-06-20',
                'end_date' => '2025-06-30',
            ],
            [
                'id' => 3,
                'name' => 'Movie Promo',
                'type' => 'image',
                'url_type' => 'url',
                'placement' => 'sidebar',
                'media' => 'https://image.tmdb.org/t/p/w780/or06FN3Dka5tukK1e9sl16pB3iy.jpg',
                'redirect_url' => 'https://image.tmdb.org/t/p/w780/or06FN3Dka5tukK1e9sl16pB3iy.jpg',
                'duration' => '00:10',
                'skip_enabled' => 1,
                'skip_after' => '00:05',
                'target_content_type' => 'movie',
                'target_categories' => json_encode([25,28,31]),
                'max_views' => 100,
                'status' => 1,
                'start_date' => '2025-06-20',
                'end_date' => '2025-06-30',
            ],
        ];

        if (env('IS_DUMMY_DATA')) {
            foreach ($customads as $mediaData) {
                $custom = CustomAdsSetting::create($mediaData);
                 if ($mediaData['url_type'] === 'Local') {
                    $uploadedUrl = $this->uploadToSpaces($mediaData['media']);
                    if ($uploadedUrl) {
                       $custom->media = $uploadedUrl;
                       $custom->save();
                    }
                }
            }

            Schema::enableForeignKeyConstraints();
        }
    }

    private function uploadToSpaces($publicPath)
    {
        $localFilePath = public_path($publicPath);
        $remoteFilePath = 'streamit-laravel/' . basename($publicPath);

        if (file_exists($localFilePath)) {
            $disk = env('ACTIVE_STORAGE', 'local');

            if ($disk === 'local') {
                Storage::disk($disk)->put('public/' . $remoteFilePath, file_get_contents($localFilePath));
                return asset('storage/' . $remoteFilePath);
            } else {
                Storage::disk($disk)->put($remoteFilePath, file_get_contents($localFilePath));
                return Storage::disk($disk)->url($remoteFilePath);
            }
        }

        return false;
    }
}
