import { sqliteTable, text, integer, real, unique } from 'drizzle-orm/sqlite-core';
import { createId } from '@paralleldrive/cuid2';
import { sql } from 'drizzle-orm';

export const wishlists = sqliteTable('wishlists', {
  id: text('id').primaryKey().$defaultFn(() => createId()),
  name: text('name').notNull(),
  slug: text('slug').notNull().unique(),
  description: text('description'),
  preferences: text('preferences'),
  imageUrl: text('image_url'),
  isPublic: integer('is_public', { mode: 'boolean' }).notNull().default(false),
  sortOrder: integer('sort_order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
});

export const guests = sqliteTable('guests', {
  id: text('id').primaryKey().$defaultFn(() => createId()),
  name: text('name').notNull(),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
});

export const wishlistItems = sqliteTable('wishlist_items', {
  id: text('id').primaryKey().$defaultFn(() => createId()),
  wishlistId: text('wishlist_id').notNull().references(() => wishlists.id, { onDelete: 'cascade' }),
  name: text('name').notNull(),
  description: text('description'),
  price: real('price'),
  currency: text('currency').notNull().default('USD'),
  quantity: integer('quantity').notNull().default(1),
  imageUrl: text('images'),
  purchaseUrls: text('purchase_urls', { mode: 'json' }).$type<Array<{
    label: string;
    url: string;
  }>>(),
  isArchived: integer('is_archived', { mode: 'boolean' }).notNull().default(false),
  // claim ownership lives in item_claims (1 item -> many claims; unique per (item, guest))
  sortOrder: integer('sort_order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
});

export const itemClaims = sqliteTable(
  'item_claims',
  {
    id: text('id').primaryKey().$defaultFn(() => createId()),
    itemId: text('item_id').notNull().references(() => wishlistItems.id, { onDelete: 'cascade' }),
    guestId: text('guest_id').notNull().references(() => guests.id, { onDelete: 'cascade' }),
    quantity: integer('quantity').notNull().default(1),
    note: text('note'),
    isPurchased: integer('is_purchased', { mode: 'boolean' }).notNull().default(false),
    claimedAt: integer('claimed_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
    updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
  },
  (t) => ({
    uniqueItemGuest: unique('item_claims_item_guest_unique').on(t.itemId, t.guestId),
  })
);

export const settings = sqliteTable('settings', {
  id: text('id').primaryKey().$defaultFn(() => createId()),
  key: text('key').notNull().unique(),
  value: text('value').notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
});

export type Wishlist = typeof wishlists.$inferSelect;
export type WishlistItem = typeof wishlistItems.$inferSelect;
export type Guest = typeof guests.$inferSelect;
export type ItemClaim = typeof itemClaims.$inferSelect;
export type Setting = typeof settings.$inferSelect;
