<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SourceSeeder extends Seeder
{
    public function run(): void
    {
        // Helper to get country/category IDs
        $country  = fn($slug) => DB::table('countries')->where('slug', $slug)->value('id');
        $category = fn($slug) => DB::table('categories')->where('slug', $slug)->value('id');

        $sources = [

            // ── AFRICA → Nigeria ─────────────────────────────────────────────
            ['country' => 'nigeria', 'category' => 'sports',       'name' => 'Completesports',   'rss_url' => 'https://www.completesports.com/feed/'],
            ['country' => 'nigeria', 'category' => 'sports',       'name' => 'SportingLife NG',  'rss_url' => 'https://sportinglife.ng/feed/'],
            ['country' => 'nigeria', 'category' => 'entertainment','name' => 'BellaNaija',       'rss_url' => 'https://www.bellanaija.com/feed/'],
            ['country' => 'nigeria', 'category' => 'entertainment','name' => 'Pulse Nigeria',    'rss_url' => 'https://www.pulse.ng/entertainment/rss'],
            ['country' => 'nigeria', 'category' => 'technology',   'name' => 'TechCabal',        'rss_url' => 'https://techcabal.com/feed/'],
            ['country' => 'nigeria', 'category' => 'technology',   'name' => 'Techpoint Africa', 'rss_url' => 'https://techpoint.africa/feed/'],
            ['country' => 'nigeria', 'category' => 'breaking-news','name' => 'Punch Nigeria',    'rss_url' => 'https://punchng.com/feed/'],
            ['country' => 'nigeria', 'category' => 'breaking-news','name' => 'Vanguard Nigeria', 'rss_url' => 'https://www.vanguardngr.com/feed/'],
            ['country' => 'nigeria', 'category' => 'politics',     'name' => 'Premium Times',    'rss_url' => 'https://www.premiumtimesng.com/feed/'],
            ['country' => 'nigeria', 'category' => 'politics',     'name' => 'Daily Trust',      'rss_url' => 'https://dailytrust.com/feed/'],

            // ── AFRICA → South Africa ────────────────────────────────────────
            ['country' => 'south-africa', 'category' => 'sports',       'name' => 'Sport24',         'rss_url' => 'https://www.sport24.co.za/rss'],
            ['country' => 'south-africa', 'category' => 'entertainment','name' => 'Channel24',       'rss_url' => 'https://www.channel24.co.za/rss'],
            ['country' => 'south-africa', 'category' => 'technology',   'name' => 'MyBroadband',     'rss_url' => 'https://mybroadband.co.za/news/feed'],
            ['country' => 'south-africa', 'category' => 'breaking-news','name' => 'News24',          'rss_url' => 'https://feeds.news24.com/articles/news24/TopStories/rss'],
            ['country' => 'south-africa', 'category' => 'politics',     'name' => 'Mail & Guardian', 'rss_url' => 'https://mg.co.za/feed/'],

            // ── AFRICA → Ghana ───────────────────────────────────────────────
            ['country' => 'ghana', 'category' => 'breaking-news','name' => 'Myjoyonline',     'rss_url' => 'https://www.myjoyonline.com/feed/'],
            ['country' => 'ghana', 'category' => 'sports',       'name' => 'GhanaWeb Sports', 'rss_url' => 'https://www.ghanaweb.com/GhanaHomePage/soccer/rss_soccer.php'],

            // ── AFRICA → Kenya ───────────────────────────────────────────────
            ['country' => 'kenya', 'category' => 'breaking-news','name' => 'Daily Nation',    'rss_url' => 'https://nation.africa/kenya/rss'],
            ['country' => 'kenya', 'category' => 'technology',   'name' => 'Techweez',        'rss_url' => 'https://techweez.com/feed/'],

            // ── AFRICA → Egypt ───────────────────────────────────────────────
            ['country' => 'egypt', 'category' => 'breaking-news','name' => 'Egypt Independent','rss_url' => 'https://egyptindependent.com/feed/'],

            // ── NORTH AMERICA → United States ────────────────────────────────
            ['country' => 'united-states', 'category' => 'sports',       'name' => 'ESPN',             'rss_url' => 'https://www.espn.com/espn/rss/news'],
            ['country' => 'united-states', 'category' => 'sports',       'name' => 'Bleacher Report',  'rss_url' => 'https://bleacherreport.com/articles/feed'],
            ['country' => 'united-states', 'category' => 'sports',       'name' => 'CBS Sports',       'rss_url' => 'https://www.cbssports.com/rss/headlines/'],
            ['country' => 'united-states', 'category' => 'entertainment','name' => 'Variety',          'rss_url' => 'https://variety.com/feed/'],
            ['country' => 'united-states', 'category' => 'entertainment','name' => 'TMZ',              'rss_url' => 'https://www.tmz.com/rss.xml'],
            ['country' => 'united-states', 'category' => 'entertainment','name' => 'Rolling Stone',    'rss_url' => 'https://www.rollingstone.com/feed/'],
            ['country' => 'united-states', 'category' => 'technology',   'name' => 'TechCrunch',       'rss_url' => 'https://techcrunch.com/feed/'],
            ['country' => 'united-states', 'category' => 'technology',   'name' => 'The Verge',        'rss_url' => 'https://www.theverge.com/rss/index.xml'],
            ['country' => 'united-states', 'category' => 'technology',   'name' => 'Wired US',         'rss_url' => 'https://www.wired.com/feed/rss'],
            ['country' => 'united-states', 'category' => 'technology',   'name' => 'Ars Technica',     'rss_url' => 'https://feeds.arstechnica.com/arstechnica/index'],
            ['country' => 'united-states', 'category' => 'breaking-news','name' => 'CNN',              'rss_url' => 'http://rss.cnn.com/rss/edition.rss'],
            ['country' => 'united-states', 'category' => 'breaking-news','name' => 'NPR',              'rss_url' => 'https://feeds.npr.org/1001/rss.xml'],
            ['country' => 'united-states', 'category' => 'politics',     'name' => 'The Hill',         'rss_url' => 'https://thehill.com/feed/'],
            ['country' => 'united-states', 'category' => 'politics',     'name' => 'NPR Politics',     'rss_url' => 'https://feeds.npr.org/1014/rss.xml'],

            // ── NORTH AMERICA → Canada ───────────────────────────────────────
            ['country' => 'canada', 'category' => 'sports',       'name' => 'Sportsnet',       'rss_url' => 'https://www.sportsnet.ca/feed/'],
            ['country' => 'canada', 'category' => 'breaking-news','name' => 'CBC News',         'rss_url' => 'https://www.cbc.ca/cmlink/rss-topstories'],
            ['country' => 'canada', 'category' => 'technology',   'name' => 'MobileSyrup',      'rss_url' => 'https://mobilesyrup.com/feed/'],

            // ── NORTH AMERICA → Mexico ───────────────────────────────────────
            ['country' => 'mexico', 'category' => 'sports',       'name' => 'Medio Tiempo',    'rss_url' => 'https://www.mediotiempo.com/rss.xml'],
            ['country' => 'mexico', 'category' => 'breaking-news','name' => 'El Universal MX', 'rss_url' => 'https://www.eluniversal.com.mx/rss.xml'],
            ['country' => 'mexico', 'category' => 'politics',     'name' => 'Animal Politico', 'rss_url' => 'https://www.animalpolitico.com/feed/'],

            // ── EUROPE → United Kingdom ──────────────────────────────────────
            ['country' => 'united-kingdom', 'category' => 'sports',       'name' => 'BBC Sport',      'rss_url' => 'http://feeds.bbci.co.uk/sport/rss.xml'],
            ['country' => 'united-kingdom', 'category' => 'sports',       'name' => 'Sky Sports',     'rss_url' => 'https://www.skysports.com/rss/12040'],
            ['country' => 'united-kingdom', 'category' => 'entertainment','name' => 'NME',            'rss_url' => 'https://www.nme.com/feed'],
            ['country' => 'united-kingdom', 'category' => 'technology',   'name' => 'TechRadar',      'rss_url' => 'https://www.techradar.com/rss'],
            ['country' => 'united-kingdom', 'category' => 'technology',   'name' => 'The Register',   'rss_url' => 'https://www.theregister.com/headlines.atom'],
            ['country' => 'united-kingdom', 'category' => 'breaking-news','name' => 'BBC News',       'rss_url' => 'http://feeds.bbci.co.uk/news/rss.xml'],
            ['country' => 'united-kingdom', 'category' => 'breaking-news','name' => 'Sky News',       'rss_url' => 'https://feeds.skynews.com/feeds/rss/home.xml'],
            ['country' => 'united-kingdom', 'category' => 'politics',     'name' => 'The Guardian',   'rss_url' => 'https://www.theguardian.com/politics/rss'],

            // ── EUROPE → France ──────────────────────────────────────────────
            ['country' => 'france', 'category' => 'sports',       'name' => "L'Equipe",        'rss_url' => 'https://www.lequipe.fr/rss/actu_rss.xml'],
            ['country' => 'france', 'category' => 'breaking-news','name' => 'France 24',       'rss_url' => 'https://www.france24.com/en/rss'],
            ['country' => 'france', 'category' => 'technology',   'name' => 'ZDNet France',    'rss_url' => 'https://www.zdnet.fr/feeds/rss/actualites/'],

            // ── EUROPE → Germany ─────────────────────────────────────────────
            ['country' => 'germany', 'category' => 'sports',       'name' => 'Kicker',          'rss_url' => 'https://www.kicker.de/news/fussball/bundesliga/startseite/top-news/rss.xml'],
            ['country' => 'germany', 'category' => 'breaking-news','name' => 'DW News',         'rss_url' => 'https://rss.dw.com/rdf/rss-en-all'],
            ['country' => 'germany', 'category' => 'technology',   'name' => 'Heise Online',    'rss_url' => 'https://www.heise.de/rss/heise-top-atom.xml'],

            // ── EUROPE → Spain ───────────────────────────────────────────────
            ['country' => 'spain', 'category' => 'sports',       'name' => 'Marca',           'rss_url' => 'https://www.marca.com/rss/portada.xml'],
            ['country' => 'spain', 'category' => 'sports',       'name' => 'AS Spain',        'rss_url' => 'https://as.com/rss/tags/ultimas_noticias.xml'],
            ['country' => 'spain', 'category' => 'breaking-news','name' => 'El Pais',         'rss_url' => 'https://feeds.elpais.com/mrss-s/pages/ep/site/elpais.com/portada'],
            ['country' => 'spain', 'category' => 'technology',   'name' => 'Xataka',          'rss_url' => 'https://www.xataka.com/index.xml'],

            // ── EUROPE → Italy ───────────────────────────────────────────────
            ['country' => 'italy', 'category' => 'sports',       'name' => 'Gazzetta dello Sport','rss_url' => 'https://www.gazzetta.it/rss/home.xml'],
            ['country' => 'italy', 'category' => 'breaking-news','name' => 'La Repubblica',   'rss_url' => 'https://www.repubblica.it/rss/homepage/rss2.0.xml'],

            // ── ASIA → India ─────────────────────────────────────────────────
            ['country' => 'india', 'category' => 'sports',       'name' => 'ESPN Cricinfo',   'rss_url' => 'https://www.espncricinfo.com/rss/content/story/feeds/0.xml'],
            ['country' => 'india', 'category' => 'entertainment','name' => 'Bollywood Hungama','rss_url' => 'https://www.bollywoodhungama.com/rss/'],
            ['country' => 'india', 'category' => 'entertainment','name' => 'Pinkvilla',       'rss_url' => 'https://www.pinkvilla.com/rss.xml'],
            ['country' => 'india', 'category' => 'technology',   'name' => 'Gadgets360',      'rss_url' => 'https://gadgets.ndtv.com/rss/feeds'],
            ['country' => 'india', 'category' => 'technology',   'name' => 'Inc42',           'rss_url' => 'https://inc42.com/feed/'],
            ['country' => 'india', 'category' => 'breaking-news','name' => 'NDTV',            'rss_url' => 'https://feeds.feedburner.com/ndtvnews-top-stories'],
            ['country' => 'india', 'category' => 'politics',     'name' => 'The Wire India',  'rss_url' => 'https://thewire.in/feed'],

            // ── ASIA → Middle East ───────────────────────────────────────────
            ['country' => 'middle-east', 'category' => 'breaking-news','name' => 'Al Jazeera',     'rss_url' => 'https://www.aljazeera.com/xml/rss/all.xml'],
            ['country' => 'middle-east', 'category' => 'breaking-news','name' => 'Arab News',      'rss_url' => 'https://www.arabnews.com/rss.xml'],
            ['country' => 'middle-east', 'category' => 'politics',     'name' => 'Middle East Eye','rss_url' => 'https://www.middleeasteye.net/rss'],
            ['country' => 'middle-east', 'category' => 'technology',   'name' => 'Wamda MENA',     'rss_url' => 'https://www.wamda.com/feed'],

            // ── ASIA → Japan ─────────────────────────────────────────────────
            ['country' => 'japan', 'category' => 'breaking-news','name' => 'NHK World',       'rss_url' => 'https://www3.nhk.or.jp/rj/podcast/r/hwnews.xml'],
            ['country' => 'japan', 'category' => 'entertainment','name' => 'Anime News Network','rss_url' => 'https://www.animenewsnetwork.com/all/rss.xml'],

            // ── SOUTH AMERICA → Brazil ───────────────────────────────────────
            ['country' => 'brazil', 'category' => 'sports',       'name' => 'Globo Esporte',   'rss_url' => 'https://ge.globo.com/rss/feed.xml'],
            ['country' => 'brazil', 'category' => 'sports',       'name' => 'Lance!',          'rss_url' => 'https://www.lance.com.br/rss.xml'],
            ['country' => 'brazil', 'category' => 'technology',   'name' => 'Tecmundo',        'rss_url' => 'https://rss.tecmundo.com.br/feed'],
            ['country' => 'brazil', 'category' => 'breaking-news','name' => 'G1 Globo',        'rss_url' => 'https://g1.globo.com/rss/g1/index.xml'],
            ['country' => 'brazil', 'category' => 'politics',     'name' => 'Agencia Publica', 'rss_url' => 'https://apublica.org/feed/'],

            // ── SOUTH AMERICA → Argentina ────────────────────────────────────
            ['country' => 'argentina', 'category' => 'sports',       'name' => 'Olé',          'rss_url' => 'https://www.ole.com.ar/rss/ultimas-noticias/'],
            ['country' => 'argentina', 'category' => 'breaking-news','name' => 'Infobae',      'rss_url' => 'https://www.infobae.com/feeds/rss/'],
            ['country' => 'argentina', 'category' => 'politics',     'name' => 'Pagina 12',    'rss_url' => 'https://www.pagina12.com.ar/rss/portada'],

            // ── SOUTH AMERICA → Colombia ─────────────────────────────────────
            ['country' => 'colombia', 'category' => 'sports',       'name' => 'Futbolred',     'rss_url' => 'https://www.futbolred.com/rss/ultimas-noticias.xml'],
            ['country' => 'colombia', 'category' => 'breaking-news','name' => 'El Tiempo',     'rss_url' => 'https://www.eltiempo.com/rss/portada.xml'],
            ['country' => 'colombia', 'category' => 'politics',     'name' => 'La Silla Vacia','rss_url' => 'https://lasillavacia.com/feed'],

            // ── OCEANIA → Australia ──────────────────────────────────────────
            ['country' => 'australia', 'category' => 'sports',       'name' => 'Fox Sports AU',  'rss_url' => 'https://www.foxsports.com.au/rss'],
            ['country' => 'australia', 'category' => 'entertainment','name' => 'Gizmodo AU',     'rss_url' => 'https://www.gizmodo.com.au/feed/'],
            ['country' => 'australia', 'category' => 'technology',   'name' => 'IT News AU',     'rss_url' => 'https://www.itnews.com.au/rss/'],
            ['country' => 'australia', 'category' => 'breaking-news','name' => 'ABC News AU',    'rss_url' => 'https://www.abc.net.au/news/feed/51120/rss.xml'],
            ['country' => 'australia', 'category' => 'politics',     'name' => 'Crikey',         'rss_url' => 'https://www.crikey.com.au/feed/'],

            // ── OCEANIA → New Zealand ────────────────────────────────────────
            ['country' => 'new-zealand', 'category' => 'breaking-news','name' => 'NZ Herald',   'rss_url' => 'https://www.nzherald.co.nz/arc/outboundfeeds/rss/'],
            ['country' => 'new-zealand', 'category' => 'politics',     'name' => 'RNZ Politics', 'rss_url' => 'https://www.rnz.co.nz/rss/political.xml'],
        ];

        $count = 0;
        foreach ($sources as $source) {
            $countryId  = $country($source['country']);
            $categoryId = $category($source['category']);

            if (!$countryId || !$categoryId) {
                $this->command->warn("  Skipping {$source['name']} — country/category not found");
                continue;
            }

            DB::table('sources')->updateOrInsert(
                ['name' => $source['name'], 'country_id' => $countryId],
                [
                    'country_id'      => $countryId,
                    'category_id'     => $categoryId,
                    'name'            => $source['name'],
                    'rss_url'         => $source['rss_url'],
                    'website_url'     => $source['website_url'] ?? null,
                    'is_active'       => true,
                    'articles_fetched'=> 0,
                    'created_at'      => now(),
                    'updated_at'      => now(),
                ]
            );
            $count++;
        }

        $this->command->info("✓ Sources seeded ({$count})");
    }
}
