<?php

namespace Database\Seeders;

use App\Models\Feature;
use App\Models\FeatureGroup;
use App\Models\Plan;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class FeatureSeeder extends Seeder
{
    public function run(): void
    {
        // ── Feature Groups (upsert) ──────────────────────────────────

        $organisation = FeatureGroup::updateOrCreate(
            ['key' => 'organisation'],
            ['label' => 'Organisation', 'sort' => 1]
        );

        $productivity = FeatureGroup::updateOrCreate(
            ['key' => 'productivity'],
            ['label' => 'Produktivität', 'sort' => 2]
        );

        $integration = FeatureGroup::updateOrCreate(
            ['key' => 'integration'],
            ['label' => 'Integrationen', 'sort' => 3]
        );

        $performance = FeatureGroup::updateOrCreate(
            ['key' => 'performance'],
            ['label' => 'Performance', 'sort' => 4]
        );

        // ── Features (upsert) ────────────────────────────────────────

        $features = [
            ['key' => 'calendar',       'label' => 'Kalender & Termine',          'icon' => 'heroicon-o-calendar',      'group' => $organisation, 'sort' => 1],
            ['key' => 'reminders',      'label' => 'Erinnerungen',                'icon' => 'heroicon-o-bell',          'group' => $organisation, 'sort' => 2],
            ['key' => 'tasks',          'label' => 'Aufgaben',                    'icon' => 'heroicon-o-check-circle',  'group' => $organisation, 'sort' => 3],
            ['key' => 'notes',          'label' => 'Notizen',                     'icon' => 'heroicon-o-document-text', 'group' => $productivity, 'sort' => 1],
            ['key' => 'contacts',       'label' => 'Kontakte',                    'icon' => 'heroicon-o-users',         'group' => $productivity, 'sort' => 2],
            ['key' => 'ai_agent',       'label' => 'KI-Assistent',               'icon' => 'heroicon-o-sparkles',      'group' => $productivity, 'sort' => 3],
            ['key' => 'calendar_sync',  'label' => 'Kalender-Synchronisierung',   'icon' => 'heroicon-o-arrow-path',    'group' => $integration,  'sort' => 1],
            ['key' => 'automations',    'label' => 'Automationen',                'icon' => 'heroicon-o-bolt',          'group' => $integration,  'sort' => 2],
            ['key' => 'speed',          'label' => 'GPT-4o Modell (schneller & präziser)', 'icon' => 'heroicon-o-bolt', 'group' => $performance,  'sort' => 1],
        ];

        $featureIds = [];
        foreach ($features as $f) {
            $feature = Feature::updateOrCreate(
                ['key' => $f['key']],
                [
                    'label'            => $f['label'],
                    'icon'             => $f['icon'],
                    'feature_group_id' => $f['group']->id,
                    'sort'             => $f['sort'],
                    'active'           => true,
                ]
            );
            $featureIds[$f['key']] = $feature->id;
        }

        // ── Plan → Feature Zuordnung ─────────────────────────────────

        // Features die jeder Plan bekommt (Free + Pro)
        $freeFeatures = ['calendar', 'reminders', 'tasks', 'notes', 'contacts', 'ai_agent'];

        // Features die nur Pro bekommt
        $proFeatures = ['calendar_sync', 'automations', 'speed'];

        // Bestehende Verknüpfungen leeren
        DB::table('feature_plan')->delete();

        // Alle Pläne laden
        $plans = Plan::all()->keyBy('plan_key');

        foreach ($plans as $planKey => $plan) {
            $assignFeatures = $freeFeatures;

            // Pro-Pläne und interne Pläne bekommen alle Features
            if ($plan->price > 0 || $plan->is_internal) {
                $assignFeatures = array_merge($assignFeatures, $proFeatures);
            }

            foreach ($assignFeatures as $featureKey) {
                if (!isset($featureIds[$featureKey])) continue;

                DB::table('feature_plan')->insert([
                    'plan_id'    => $plan->id,
                    'feature_id' => $featureIds[$featureKey],
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }

            $this->command->info("{$plan->name}: " . count($assignFeatures) . " Features zugewiesen");
        }
    }
}
