export interface MockList {
  id: string;
  name: string;
  color: string;
  icon: string;
  _count?: { tasks: number };
}

export interface MockTask {
  id: string;
  listId: string;
  parentId: string | null;
  title: string;
  note: string | null;
  completed: boolean;
  completedAt: string | null;
  dueDate: string | null;
  priority: number;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  children: MockTask[];
  tags: { tag: { id: string; name: string; color: string } }[];
}

const STORAGE_KEY_LISTS = "checkflow_demo_lists";
const STORAGE_KEY_TASKS = "checkflow_demo_tasks";

export const INITIAL_DEMO_LISTS: MockList[] = [
  { id: "list-1", name: "🚀 Project Launch", color: "#4B7BF5", icon: "work" },
  { id: "list-2", name: "🏠 Personal & Daily", color: "#10B981", icon: "personal" },
  { id: "list-3", name: "📚 Tech Research", color: "#8B5CF6", icon: "study" },
];

export const INITIAL_DEMO_TASKS: MockTask[] = [
  {
    id: "task-1",
    listId: "list-1",
    parentId: null,
    title: "Setup Self-Hosted CheckFlow on NAS",
    note: "## 🐳 Docker Deployment Guide\n\n- Deploy with `docker compose up -d`\n- Forward port 3000 to NPM (Nginx Proxy Manager)\n- Setup SSL certificate for custom domain\n\n### 🔗 Key Endpoints\n- Web App: `https://todo.yourdomain.com`\n- CardDAV: `https://todo.yourdomain.com/api/dav`",
    completed: false,
    completedAt: null,
    dueDate: new Date(Date.now() + 86400000).toISOString(),
    priority: 3,
    sortOrder: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    children: [
      {
        id: "sub-1-1",
        listId: "list-1",
        parentId: "task-1",
        title: "Configure .env environment variables",
        note: null,
        completed: true,
        completedAt: new Date().toISOString(),
        dueDate: null,
        priority: 0,
        sortOrder: 0,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        children: [],
        tags: [],
      },
      {
        id: "sub-1-2",
        listId: "list-1",
        parentId: "task-1",
        title: "Test DAVx⁵ sync on Samsung Galaxy",
        note: null,
        completed: false,
        completedAt: null,
        dueDate: null,
        priority: 2,
        sortOrder: 1,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        children: [],
        tags: [],
      },
    ],
    tags: [],
  },
  {
    id: "task-2",
    listId: "list-1",
    parentId: null,
    title: "Import existing tasks from TickTick",
    note: "1. Export CSV/ICS in TickTick Settings\n2. Click 'Import Tasks' in CheckFlow Sidebar\n3. Select target project",
    completed: false,
    completedAt: null,
    dueDate: null,
    priority: 2,
    sortOrder: 1,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    children: [],
    tags: [],
  },
  {
    id: "task-3",
    listId: "list-2",
    parentId: null,
    title: "Install CheckFlow PWA on mobile home screen",
    note: "Open in Chrome/Samsung Internet -> Menu -> **Install app** or **Add to Home Screen**",
    completed: true,
    completedAt: new Date().toISOString(),
    dueDate: new Date().toISOString(),
    priority: 1,
    sortOrder: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
    children: [],
    tags: [],
  },
];

export function getDemoStore() {
  if (typeof window === "undefined") {
    return { lists: INITIAL_DEMO_LISTS, tasks: INITIAL_DEMO_TASKS };
  }

  let lists: MockList[] = INITIAL_DEMO_LISTS;
  let tasks: MockTask[] = INITIAL_DEMO_TASKS;

  try {
    const savedLists = localStorage.getItem(STORAGE_KEY_LISTS);
    if (savedLists) lists = JSON.parse(savedLists);
    else localStorage.setItem(STORAGE_KEY_LISTS, JSON.stringify(INITIAL_DEMO_LISTS));

    const savedTasks = localStorage.getItem(STORAGE_KEY_TASKS);
    if (savedTasks) tasks = JSON.parse(savedTasks);
    else localStorage.setItem(STORAGE_KEY_TASKS, JSON.stringify(INITIAL_DEMO_TASKS));
  } catch (e) {
    console.error("Failed to read demo storage", e);
  }

  return { lists, tasks };
}

export function saveDemoStore(lists: MockList[], tasks: MockTask[]) {
  if (typeof window === "undefined") return;
  try {
    localStorage.setItem(STORAGE_KEY_LISTS, JSON.stringify(lists));
    localStorage.setItem(STORAGE_KEY_TASKS, JSON.stringify(tasks));
  } catch (e) {
    console.error("Failed to save demo storage", e);
  }
}