import { boolean, integer, jsonb, pgTable, text, uniqueIndex } from "drizzle-orm/pg-core";
import { createdAt, updatedAt } from "./_shared.js";

type DateRule =
  | { kind: "fixed"; monthDay: string }
  | { kind: "range"; startMonthDay: string; endMonthDay: string }
  | { kind: "computed"; algorithm: "midsummer" | "easter" | "advent" | "custom" };

/**
 * Datadriven Season & Events Engine (spec §28): midsommar, jul, påsk, kräftskiva,
 * Ramadan, Eid, Thanksgiving m.fl. – per marknad, utan hårdkodning i motorerna.
 */
export const seasonEvents = pgTable(
  "season_events",
  {
    id: text("id").primaryKey(),
    slug: text("slug").notNull(),
    nameSv: text("name_sv").notNull(),
    market: text("market").notNull().default("SE"),
    dateRule: jsonb("date_rule").$type<DateRule>().notNull(),
    leadDays: integer("lead_days").notNull().default(7),
    foodTags: text("food_tags").array().notNull().default([]),
    recipeSlugs: text("recipe_slugs").array().notNull().default([]),
    priority: integer("priority").notNull().default(0),
    active: boolean("active").notNull().default(true),
    createdAt: createdAt(),
    updatedAt: updatedAt(),
  },
  (t) => [uniqueIndex("season_events_slug_market_unique").on(t.slug, t.market)],
);
