<?php

namespace Database\Seeders;

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

class SamplePlanSeeder extends Seeder
{
    public function run(): void
    {
        $now = now();

        $categoryIds = DB::table('categories')->pluck('id', 'slug');
        $networkIds = DB::table('networks')->pluck('id', 'slug');

        $records = [];
        $sort = 10;

        $dataTemplates = [
            ['name_suffix' => '500MB', 'value' => '500MB', 'base_price' => 250.00],
            ['name_suffix' => '1GB', 'value' => '1GB', 'base_price' => 480.00],
            ['name_suffix' => '2GB', 'value' => '2GB', 'base_price' => 960.00],
        ];

        $dataCategories = [
            'data_sme' => 0,
            'data_gifting' => 30,
            'data_cg' => 50,
            'data_direct' => 80,
        ];

        foreach ($networkIds as $networkSlug => $networkId) {
            $networkName = strtoupper($networkSlug === '9mobile' ? '9mobile' : $networkSlug);

            foreach ($dataCategories as $categorySlug => $markup) {
                foreach ($dataTemplates as $template) {
                    $records[] = [
                        'network_id' => $networkId,
                        'category_id' => $categoryIds[$categorySlug],
                        'force_vendor_id' => null,
                        'name' => "{$networkName} {$this->categoryLabel($categorySlug)} {$template['name_suffix']}",
                        'value' => $template['value'],
                        'selling_price' => $template['base_price'] + $markup,
                        'status' => 'available',
                        'sort_order' => $sort++,
                        'meta_json' => json_encode([
                            'sample' => true,
                            'service' => 'data',
                            'network' => $networkSlug,
                            'category' => $categorySlug,
                            'vtpass_service_id' => $this->vtpassDataServiceId($networkSlug, $categorySlug),
                            'vendor_codes' => [
                                'msorg' => "{$networkSlug}-{$categorySlug}-".strtolower($template['name_suffix']),
                                'smeplug' => null,
                            ],
                        ], JSON_THROW_ON_ERROR),
                        'created_at' => $now,
                        'updated_at' => $now,
                    ];
                }
            }

            $records[] = [
                'network_id' => $networkId,
                'category_id' => $categoryIds['airtime_vtu'],
                'force_vendor_id' => null,
                'name' => "{$networkName} Airtime VTU",
                'value' => 'VARIABLE',
                'selling_price' => 0,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode([
                    'sample' => true,
                    'service' => 'airtime',
                    'variable_amount' => true,
                    'min_amount' => 50,
                    'max_amount' => 50000,
                ], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        $records = array_merge($records, [
            [
                'network_id' => null,
                'category_id' => $categoryIds['cable'],
                'force_vendor_id' => null,
                'name' => 'DStv Compact',
                'value' => 'DSTV-COMPACT',
                'selling_price' => 15700.00,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode(['sample' => true, 'service' => 'cable', 'cablename' => 'dstv', 'vtpass_service_id' => 'dstv', 'vtpass_subscription_type' => 'change'], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['cable'],
                'force_vendor_id' => null,
                'name' => 'GOtv Max',
                'value' => 'GOTV-MAX',
                'selling_price' => 7200.00,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode(['sample' => true, 'service' => 'cable', 'cablename' => 'gotv', 'vtpass_service_id' => 'gotv', 'vtpass_subscription_type' => 'change'], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['electricity'],
                'force_vendor_id' => null,
                'name' => 'Electricity Prepaid Token',
                'value' => 'PREPAID',
                'selling_price' => 0,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode([
                    'sample' => true,
                    'service' => 'electricity',
                    'variable_amount' => true,
                    'meter_type' => 1,
                    'vtpass_service_id' => 'ikeja-electric',
                ], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['electricity'],
                'force_vendor_id' => null,
                'name' => 'Electricity Postpaid Bill',
                'value' => 'POSTPAID',
                'selling_price' => 0,
                'status' => 'maintenance',
                'sort_order' => $sort++,
                'meta_json' => json_encode([
                    'sample' => true,
                    'service' => 'electricity',
                    'variable_amount' => true,
                    'meter_type' => 2,
                    'vtpass_service_id' => 'ikeja-electric',
                ], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['epin'],
                'force_vendor_id' => null,
                'name' => 'WAEC ePIN',
                'value' => 'WAEC',
                'selling_price' => 4800.00,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode(['sample' => true, 'service' => 'epin', 'exam_name' => 'waec', 'vtpass_service_id' => 'waec'], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['epin'],
                'force_vendor_id' => null,
                'name' => 'NECO ePIN',
                'value' => 'NECO',
                'selling_price' => 1600.00,
                'status' => 'available',
                'sort_order' => $sort++,
                'meta_json' => json_encode(['sample' => true, 'service' => 'epin', 'exam_name' => 'neco', 'vtpass_service_id' => 'neco'], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'network_id' => null,
                'category_id' => $categoryIds['epin'],
                'force_vendor_id' => null,
                'name' => 'NABTEB ePIN',
                'value' => 'NABTEB',
                'selling_price' => 1800.00,
                'status' => 'hidden',
                'sort_order' => $sort++,
                'meta_json' => json_encode(['sample' => true, 'service' => 'epin', 'exam_name' => 'nabteb', 'vtpass_service_id' => 'nabteb'], JSON_THROW_ON_ERROR),
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ]);

        foreach ($records as $record) {
            DB::table('plans')->updateOrInsert(
                [
                    'name' => $record['name'],
                    'category_id' => $record['category_id'],
                    'network_id' => $record['network_id'],
                ],
                $record
            );
        }
    }

    private function categoryLabel(string $slug): string
    {
        return match ($slug) {
            'data_sme' => 'SME',
            'data_gifting' => 'Gifting',
            'data_cg' => 'CG',
            'data_direct' => 'Direct',
            default => ucfirst(str_replace('_', ' ', $slug)),
        };
    }

    private function vtpassDataServiceId(string $networkSlug, string $categorySlug): string
    {
        return match ($networkSlug) {
            'mtn' => 'mtn-data',
            'airtel' => 'airtel-data',
            'glo' => $categorySlug === 'data_sme' ? 'glo-sme-data' : 'glo-data',
            '9mobile' => 'etisalat-data',
            default => 'mtn-data',
        };
    }
}
