import { ledgerThemeAccents, type CurrencyCode, type LedgerAmountDisplayMode, type LedgerIconKey, type LedgerThemeId } from "@cents/domain";

export type LedgerSummaryRange = "ledger" | "year";

export const ledgerThemes: ReadonlyArray<{
  id: LedgerThemeId;
  name: string;
  accent: string;
  gradient: string;
}> = [
  { id: "jade", name: "翡翠", accent: ledgerThemeAccents.jade, gradient: "linear-gradient(135deg, #118b79 0%, #087f72 48%, #285765 100%)" },
  { id: "ocean", name: "海湾", accent: ledgerThemeAccents.ocean, gradient: "linear-gradient(135deg, #167b93 0%, #3459a4 52%, #4b3f7f 100%)" },
  { id: "coral", name: "晚霞", accent: ledgerThemeAccents.coral, gradient: "linear-gradient(135deg, #c94f45 0%, #ad4869 54%, #6e4d82 100%)" },
  { id: "berry", name: "莓果", accent: ledgerThemeAccents.berry, gradient: "linear-gradient(135deg, #a84378 0%, #71468d 52%, #40578a 100%)" },
  { id: "meadow", name: "森野", accent: ledgerThemeAccents.meadow, gradient: "linear-gradient(135deg, #25805f 0%, #4e794b 54%, #766b35 100%)" },
  { id: "graphite", name: "暮色", accent: ledgerThemeAccents.graphite, gradient: "linear-gradient(135deg, #35585d 0%, #4e556e 52%, #303f55 100%)" },
  { id: "sunrise", name: "晨曦", accent: ledgerThemeAccents.sunrise, gradient: "linear-gradient(135deg, #d18a45 0%, #b45f4a 50%, #82506f 100%)" },
  { id: "lagoon", name: "泻湖", accent: ledgerThemeAccents.lagoon, gradient: "linear-gradient(135deg, #15988b 0%, #177f8f 50%, #3f5f91 100%)" },
  { id: "iris", name: "鸢尾", accent: ledgerThemeAccents.iris, gradient: "linear-gradient(135deg, #5b70b8 0%, #7061b5 50%, #a0508d 100%)" },
  { id: "sky", name: "晴空", accent: ledgerThemeAccents.sky, gradient: "linear-gradient(135deg, #2592a6 0%, #327ba0 52%, #49639b 100%)" },
  { id: "olive", name: "山岚", accent: ledgerThemeAccents.olive, gradient: "linear-gradient(135deg, #4c8168 0%, #64784a 52%, #8a703e 100%)" },
  { id: "midnight", name: "夜航", accent: ledgerThemeAccents.midnight, gradient: "linear-gradient(135deg, #315d70 0%, #47577f 52%, #684b78 100%)" },
  { id: "mint", name: "薄荷", accent: ledgerThemeAccents.mint, gradient: "linear-gradient(135deg, #39a889 0%, #3c9b83 52%, #357a91 100%)" },
  { id: "plum", name: "紫藤", accent: ledgerThemeAccents.plum, gradient: "linear-gradient(135deg, #a566a1 0%, #895c9c 52%, #526e9d 100%)" },
  { id: "ember", name: "余烬", accent: ledgerThemeAccents.ember, gradient: "linear-gradient(135deg, #d17b4a 0%, #b86545 52%, #765174 100%)" },
  { id: "aqua", name: "青湾", accent: ledgerThemeAccents.aqua, gradient: "linear-gradient(135deg, #38b0b1 0%, #258fa3 52%, #456ca0 100%)" },
  { id: "rose", name: "玫瑰", accent: ledgerThemeAccents.rose, gradient: "linear-gradient(135deg, #d65b75 0%, #c6536f 52%, #805684 100%)" },
  { id: "forest", name: "深林", accent: ledgerThemeAccents.forest, gradient: "linear-gradient(135deg, #5d9a6d 0%, #4b7f5c 52%, #536b46 100%)" },
];

export function ledgerTheme(themeId?: string) {
  return ledgerThemes.find((theme) => theme.id === themeId) ?? ledgerThemes[0];
}

export type LedgerRecord = {
  id: string;
  name: string;
  color: string;
  theme: LedgerThemeId;
  icon: LedgerIconKey;
  defaultCurrency: CurrencyCode;
  amountDisplay: LedgerAmountDisplayMode;
  summaryRange: LedgerSummaryRange;
  createdAt: string;
  updatedAt: string;
  archivedAt: string | null;
  role?: "owner" | "member";
  isPersonal: boolean;
};

export type UserPreference = {
  id: string;
  userId: string;
  key: "currentLedgerId" | "ledgerAmountDisplay" | "ledgerSummaryRange" | "legacyMigration";
  value: string;
  updatedAt: string;
};

export function currentLedgerPreferenceId(userId: string) {
  return `${userId}:currentLedgerId`;
}

export function ledgerAmountDisplayPreferenceId(userId: string, ledgerId: string) {
  return `${userId}:ledgerAmountDisplay:${ledgerId}`;
}

export function ledgerSummaryRangePreferenceId(userId: string, ledgerId: string) {
  return `${userId}:ledgerSummaryRange:${ledgerId}`;
}

export function createDefaultLedgers(): LedgerRecord[] {
  const now = new Date().toISOString();
  return [
    {
      id: "local-ledger",
      name: "家庭日常",
      color: "#087f72",
      theme: "jade",
      icon: "wallet",
      defaultCurrency: "CNY",
      amountDisplay: "base",
      summaryRange: "year",
      createdAt: now,
      updatedAt: now,
      archivedAt: null,
      isPersonal: true,
    },
    {
      id: "travel-ledger",
      name: "旅行账本",
      color: "#3478e5",
      theme: "ocean",
      icon: "luggage",
      defaultCurrency: "CNY",
      amountDisplay: "base",
      summaryRange: "year",
      createdAt: now,
      updatedAt: now,
      archivedAt: null,
      isPersonal: false,
    },
  ];
}
