package main

// Seed the empyrion_inv_templates table with three reasonable default kits
// (Starter Pack, Survival Crate, Admin Box) on first boot. Idempotent —
// runs only when the table is empty, so operator edits and renames are
// preserved across controller restarts.

import (
	"context"
	"encoding/json"

	"github.com/dbledeez/panel/controller/internal/db"
)

type kitItem struct {
	ItemID int `json:"itemId"`
	Count  int `json:"count"`
	Ammo   int `json:"ammo,omitempty"`
	Decay  int `json:"decay,omitempty"`
}

type kitSeed struct {
	Name        string
	Description string
	Items       []kitItem
}

var defaultEmpyrionKits = []kitSeed{
	{
		Name:        "Starter Pack",
		Description: "New-player welcome kit — basic survival tool, light, oxygen, food, water, first-aid.",
		Items: []kitItem{
			{ItemID: 707, Count: 1},  // SurvivalConstructor
			{ItemID: 39, Count: 1},   // SurvivalTool
			{ItemID: 2, Count: 1},    // Flashlight
			{ItemID: 80, Count: 2},   // OxygenBottleLarge
			{ItemID: 218, Count: 4},  // EnergyCell
			{ItemID: 346, Count: 10}, // EmergencyRations
			{ItemID: 348, Count: 4},  // WaterJug
			{ItemID: 341, Count: 5},  // Bandages
			{ItemID: 329, Count: 2},  // Medikit04 (Health Pack small)
		},
	},
	{
		Name:        "Survival Crate",
		Description: "Mid-game care package for a struggling player — armor, rifle, ample supplies, full medikit shelf.",
		Items: []kitItem{
			{ItemID: 707, Count: 1},  // SurvivalConstructor
			{ItemID: 15, Count: 1},   // AssaultRifle (T1)
			{ItemID: 600, Count: 1},  // ArmorLight
			{ItemID: 80, Count: 4},   // OxygenBottleLarge
			{ItemID: 218, Count: 10}, // EnergyCell
			{ItemID: 346, Count: 20}, // EmergencyRations
			{ItemID: 348, Count: 8},  // WaterJug
			{ItemID: 366, Count: 5},  // Medikit06 (Health Pack large)
			{ItemID: 393, Count: 3},  // AntiRadiationInjection
			{ItemID: 391, Count: 3},  // AntiToxicInjection
			{ItemID: 345, Count: 3},  // AntibioticInjection
		},
	},
	{
		Name:        "Admin Box",
		Description: "High-tier admin loadout for testing — epic weapons, heavy armor, all boosters, deep ammo + power reserves.",
		Items: []kitItem{
			{ItemID: 24, Count: 1},   // AssaultRifleEpic
			{ItemID: 30, Count: 1},   // PistolEpic
			{ItemID: 40, Count: 1},   // DrillEpic
			{ItemID: 605, Count: 1},  // ArmorHeavyEpic
			{ItemID: 650, Count: 1},  // ArmorBoostEpic
			{ItemID: 651, Count: 1},  // JetpackBoostEpic
			{ItemID: 653, Count: 1},  // OxygenBoostEpic
			{ItemID: 657, Count: 1},  // EVABoostEpic
			{ItemID: 80, Count: 10},  // OxygenBottleLarge
			{ItemID: 239, Count: 20}, // EnergyCellLarge
			{ItemID: 241, Count: 10}, // HydrogenBottle
			{ItemID: 366, Count: 10}, // Medikit06
		},
	},
}

// seedEmpyrionKits inserts the three default kits into empyrion_inv_templates
// when the table is empty. Once any row exists (whether seeded by us, created
// by the operator, or imported from another panel), the function is a no-op
// — operator-driven changes are never overwritten.
func seedEmpyrionKits(ctx context.Context, log Logger, database *db.DB) error {
	var n int
	if err := database.Pool().QueryRow(ctx, `SELECT COUNT(*) FROM empyrion_inv_templates`).Scan(&n); err != nil {
		return err
	}
	if n > 0 {
		return nil
	}
	for _, k := range defaultEmpyrionKits {
		itemsJSON, err := json.Marshal(k.Items)
		if err != nil {
			return err
		}
		if _, err := database.Pool().Exec(ctx, `
			INSERT INTO empyrion_inv_templates (name, description, items)
			VALUES ($1, $2, $3::jsonb)`,
			k.Name, k.Description, string(itemsJSON)); err != nil {
			return err
		}
	}
	log.Info("seeded default empyrion inventory templates", "count", len(defaultEmpyrionKits))
	return nil
}
