import {
  boolean,
  date,
  doublePrecision,
  index,
  integer,
  pgTable,
  text,
  uuid,
} from "drizzle-orm/pg-core";
import { createdAt, mealTypeEnum, unitEnum, updatedAt } from "./_shared.js";
import { households } from "./households.js";
import { mealBoxes } from "./meals.js";
import { canonicalIngredients } from "./ingredients.js";
import { recipes } from "./recipes.js";
import { users } from "./users.js";

/** Veckoplanering (spec §25). */
export const weekPlans = pgTable(
  "week_plans",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    householdId: uuid("household_id")
      .notNull()
      .references(() => households.id, { onDelete: "cascade" }),
    weekStartDate: date("week_start_date").notNull(),
    status: text("status").notNull().default("draft"),
    generatedBy: text("generated_by").notNull().default("user"),
    notes: text("notes"),
    createdAt: createdAt(),
    updatedAt: updatedAt(),
  },
  (t) => [index("week_plans_household_week_idx").on(t.householdId, t.weekStartDate)],
);

export const weekPlanEntries = pgTable(
  "week_plan_entries",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    weekPlanId: uuid("week_plan_id")
      .notNull()
      .references(() => weekPlans.id, { onDelete: "cascade" }),
    date: date("date").notNull(),
    mealType: mealTypeEnum("meal_type").notNull(),
    recipeId: uuid("recipe_id").references(() => recipes.id, { onDelete: "set null" }),
    mealBoxId: uuid("meal_box_id").references(() => mealBoxes.id, { onDelete: "set null" }),
    titleSv: text("title_sv").notNull(),
    portions: integer("portions").notNull().default(2),
    status: text("status").notNull().default("planned"),
    /** Förklaring vid dynamisk omplanering (spec §25). */
    rescheduleReasonSv: text("reschedule_reason_sv"),
    sortOrder: integer("sort_order").notNull().default(0),
  },
  (t) => [index("week_plan_entries_plan_idx").on(t.weekPlanId)],
);

/** Inköpslista (spec §27) – delas i hushållet, fungerar offline, uppdaterar lagret. */
export const shoppingLists = pgTable(
  "shopping_lists",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    householdId: uuid("household_id")
      .notNull()
      .references(() => households.id, { onDelete: "cascade" }),
    name: text("name").notNull().default("Inköpslista"),
    status: text("status").notNull().default("active"),
    weekPlanId: uuid("week_plan_id").references(() => weekPlans.id, { onDelete: "set null" }),
    createdAt: createdAt(),
    updatedAt: updatedAt(),
  },
  (t) => [index("shopping_lists_household_idx").on(t.householdId)],
);

export const shoppingListItems = pgTable(
  "shopping_list_items",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    shoppingListId: uuid("shopping_list_id")
      .notNull()
      .references(() => shoppingLists.id, { onDelete: "cascade" }),
    canonicalIngredientId: text("canonical_ingredient_id").references(
      () => canonicalIngredients.id,
    ),
    displayName: text("display_name").notNull(),
    quantity: doublePrecision("quantity").notNull().default(1),
    unit: unitEnum("unit").notNull().default("COUNT"),
    storeSection: text("store_section").notNull().default("hygien_ovrigt"),
    suggestedPackageSize: text("suggested_package_size"),
    estimatedPriceMinor: integer("estimated_price_minor"),
    checked: boolean("checked").notNull().default(false),
    addedByUserId: uuid("added_by_user_id").references(() => users.id, { onDelete: "set null" }),
    origin: text("origin").notNull().default("manual"),
    sortOrder: integer("sort_order").notNull().default(0),
  },
  (t) => [index("shopping_list_items_list_idx").on(t.shoppingListId)],
);
