import presetsJson from "../../data/presets.json";
import { recommendArtifacts, suggestBuilds } from "./scoring";
import type { AppSnapshot, Artifact, Character, CharacterPreset, ScanEvent } from "../types/domain";

const presets = presetsJson.characters.map((entry) => ({
  ...entry,
  characterId: entry.id,
})) as unknown as CharacterPreset[];

export function createDemoArtifacts(): Artifact[] {
  const now = new Date().toISOString();
  return [
    {
      id: "art-001",
      setKey: "marechaussee_hunter",
      setName: "Marechaussee Hunter",
      slot: "sands",
      rarity: 5,
      level: 20,
      mainStat: "HP%",
      substats: [
        { key: "CRIT Rate", value: 10.1, unit: "%" },
        { key: "CRIT DMG", value: 14.0, unit: "%" },
        { key: "Energy Recharge", value: 5.8, unit: "%" },
        { key: "ATK%", value: 4.1, unit: "%" },
      ],
      locked: true,
      source: "mock",
      confidence: 0.97,
      lastSeenAt: now,
    },
    {
      id: "art-002",
      setKey: "golden_troupe",
      setName: "Golden Troupe",
      slot: "goblet",
      rarity: 5,
      level: 16,
      mainStat: "HP%",
      substats: [
        { key: "Energy Recharge", value: 11.0, unit: "%" },
        { key: "CRIT Rate", value: 6.6, unit: "%" },
        { key: "CRIT DMG", value: 13.2, unit: "%" },
        { key: "Elemental Mastery", value: 19, unit: "flat" },
      ],
      locked: false,
      source: "mock",
      confidence: 0.94,
      lastSeenAt: now,
    },
    {
      id: "art-003",
      setKey: "emblem_of_severed_fate",
      setName: "Emblem of Severed Fate",
      slot: "circlet",
      rarity: 5,
      level: 20,
      mainStat: "CRIT Rate",
      substats: [
        { key: "Energy Recharge", value: 17.5, unit: "%" },
        { key: "CRIT DMG", value: 19.4, unit: "%" },
        { key: "ATK%", value: 5.3, unit: "%" },
        { key: "HP%", value: 4.7, unit: "%" },
      ],
      locked: true,
      source: "mock",
      confidence: 0.99,
      lastSeenAt: now,
    },
    {
      id: "art-004",
      setKey: "deepwood_memories",
      setName: "Deepwood Memories",
      slot: "goblet",
      rarity: 5,
      level: 4,
      mainStat: "Elemental Mastery",
      substats: [
        { key: "CRIT Rate", value: 3.9, unit: "%" },
        { key: "Energy Recharge", value: 6.5, unit: "%" },
        { key: "ATK%", value: 4.7, unit: "%" },
      ],
      locked: false,
      source: "mock",
      confidence: 0.91,
      lastSeenAt: now,
    },
    {
      id: "art-005",
      setKey: "viridescent_venerer",
      setName: "Viridescent Venerer",
      slot: "circlet",
      rarity: 5,
      level: 0,
      mainStat: "Elemental Mastery",
      substats: [
        { key: "Energy Recharge", value: 5.2, unit: "%" },
        { key: "DEF%", value: 7.3, unit: "%" },
        { key: "HP", value: 269, unit: "flat" },
      ],
      locked: false,
      source: "mock",
      confidence: 0.86,
      lastSeenAt: now,
    },
    {
      id: "art-006",
      setKey: "noblesse_oblige",
      setName: "Noblesse Oblige",
      slot: "sands",
      rarity: 5,
      level: 0,
      mainStat: "DEF%",
      substats: [
        { key: "DEF", value: 23, unit: "flat" },
        { key: "HP", value: 209, unit: "flat" },
        { key: "ATK", value: 19, unit: "flat" },
      ],
      locked: false,
      source: "mock",
      confidence: 0.96,
      lastSeenAt: now,
    },
    {
      id: "art-007",
      setKey: "heart_of_depth",
      setName: "Heart of Depth",
      slot: "plume",
      rarity: 5,
      level: 12,
      mainStat: "ATK",
      substats: [
        { key: "CRIT Rate", value: 6.2, unit: "%" },
        { key: "CRIT DMG", value: 12.4, unit: "%" },
        { key: "HP%", value: 9.9, unit: "%" },
        { key: "Energy Recharge", value: 4.5, unit: "%" },
      ],
      locked: false,
      source: "mock",
      confidence: 0.8,
      lastSeenAt: now,
    },
    {
      id: "art-008",
      setKey: "viridescent_venerer",
      setName: "Viridescent Venerer",
      slot: "sands",
      rarity: 5,
      level: 20,
      mainStat: "Elemental Mastery",
      substats: [
        { key: "Energy Recharge", value: 18.1, unit: "%" },
        { key: "CRIT Rate", value: 3.1, unit: "%" },
        { key: "HP%", value: 8.7, unit: "%" },
        { key: "ATK%", value: 4.7, unit: "%" },
      ],
      locked: true,
      source: "mock",
      confidence: 0.97,
      lastSeenAt: now,
    },
    {
      id: "art-009",
      setKey: "viridescent_venerer",
      setName: "Viridescent Venerer",
      slot: "flower",
      rarity: 5,
      level: 16,
      mainStat: "HP",
      substats: [
        { key: "Elemental Mastery", value: 63, unit: "flat" },
        { key: "Energy Recharge", value: 11.7, unit: "%" },
        { key: "CRIT Rate", value: 3.5, unit: "%" },
        { key: "DEF%", value: 5.8, unit: "%" },
      ],
      locked: true,
      source: "mock",
      confidence: 0.95,
      lastSeenAt: now,
    },
    {
      id: "art-010",
      setKey: "viridescent_venerer",
      setName: "Viridescent Venerer",
      slot: "plume",
      rarity: 5,
      level: 16,
      mainStat: "ATK",
      substats: [
        { key: "Elemental Mastery", value: 82, unit: "flat" },
        { key: "Energy Recharge", value: 10.4, unit: "%" },
        { key: "HP%", value: 5.3, unit: "%" },
        { key: "DEF", value: 21, unit: "flat" },
      ],
      locked: true,
      source: "mock",
      confidence: 0.94,
      lastSeenAt: now,
    },
  ];
}

export function createDemoCharacters(): Character[] {
  return [
    { id: "neuvillette", name: "Neuvillette", owned: true, level: 90, constellation: 0, rolePreference: "main_dps", confidence: 0.93 },
    { id: "furina", name: "Furina", owned: true, level: 90, constellation: 0, rolePreference: "sub_dps", confidence: 0.91 },
    { id: "raiden_shogun", name: "Raiden Shogun", owned: true, level: 80, constellation: 0, rolePreference: "main_dps", confidence: 0.88 },
    { id: "nahida", name: "Nahida", owned: true, level: 90, constellation: 0, rolePreference: "support", confidence: 0.92 },
    { id: "kazuha", name: "Kaedehara Kazuha", owned: true, level: 80, constellation: 0, rolePreference: "support", confidence: 0.87 },
  ];
}

export function createDemoScanEvents(): ScanEvent[] {
  return [
    {
      id: "scan-001",
      type: "environment",
      label: "Environment check",
      detail: "Borderless Windowed, 2560x1440 profile, English UI expected.",
      confidence: 0.92,
    },
    {
      id: "scan-002",
      type: "character",
      label: "Character pass",
      detail: "5 owned characters detected from the prepared scanner adapter.",
      confidence: 0.89,
    },
    {
      id: "scan-003",
      type: "artifact",
      label: "Artifact pass",
      detail: "7 artifacts parsed. 1 needs review because confidence is under threshold.",
      confidence: 0.9,
    },
    {
      id: "scan-004",
      type: "complete",
      label: "Account snapshot ready",
      detail: "Recommendations and build candidates are available.",
      confidence: 0.95,
    },
  ];
}

export function createDemoSnapshot(): AppSnapshot {
  const artifacts = createDemoArtifacts();
  const characters = createDemoCharacters();
  const recommendations = recommendArtifacts(artifacts, characters, presets);
  const builds = suggestBuilds(artifacts, characters, presets);

  return {
    artifacts,
    characters,
    recommendations,
    builds,
    scanEvents: createDemoScanEvents(),
  };
}

export async function runMockScan() {
  await new Promise((resolve) => window.setTimeout(resolve, 900));
  return createDemoSnapshot();
}

export function getPresets() {
  return presets;
}
