import{sql}from"drizzle-orm";import{index,integer,sqliteTable,text,uniqueIndex}from"drizzle-orm/sqlite-core";
const timestamps={createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),updatedAt:text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP`)};
export const users=sqliteTable("users",{id:text("id").primaryKey(),email:text("email").notNull(),name:text("name").notNull(),phoneMasked:text("phone_masked"),status:text("status").notNull().default("active"),emailVerifiedAt:text("email_verified_at"),...timestamps},t=>[uniqueIndex("users_email_uq").on(t.email),index("users_status_idx").on(t.status)]);
export const userRoles=sqliteTable("user_roles",{id:integer("id").primaryKey({autoIncrement:true}),userId:text("user_id").notNull().references(()=>users.id),role:text("role").notNull(),sellerId:text("seller_id"),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[uniqueIndex("user_role_uq").on(t.userId,t.role,t.sellerId)]);
export const sellers=sqliteTable("sellers",{id:text("id").primaryKey(),ownerUserId:text("owner_user_id").notNull().references(()=>users.id),displayName:text("display_name").notNull(),legalName:text("legal_name"),supportEmail:text("support_email"),status:text("status").notNull().default("draft"),kycStatus:text("kyc_status").notNull().default("not_started"),stripeAccountId:text("stripe_account_id"),payoutsEnabled:integer("payouts_enabled",{mode:"boolean"}).notNull().default(false),bankName:text("bank_name"),bankLast4:text("bank_last4"),riskLevel:text("risk_level").notNull().default("standard"),chatDefault:text("chat_default").notNull().default("buyers_only"),...timestamps},t=>[index("sellers_status_idx").on(t.status,t.kycStatus)]);
export const events=sqliteTable("events",{id:text("id").primaryKey(),sellerId:text("seller_id").notNull().references(()=>sellers.id),slug:text("slug").notNull(),title:text("title").notNull(),summary:text("summary").notNull(),description:text("description").notNull(),category:text("category").notNull(),venueName:text("venue_name"),venueAddress:text("venue_address"),city:text("city"),timezone:text("timezone").notNull(),startsAt:text("starts_at").notNull(),endsAt:text("ends_at").notNull(),doorsAt:text("doors_at"),capacity:integer("capacity").notNull(),status:text("status").notNull().default("draft"),refundPolicy:text("refund_policy").notNull(),chatMode:text("chat_mode").notNull().default("buyers_only"),heroObjectKey:text("hero_object_key"),featured:integer("featured",{mode:"boolean"}).notNull().default(false),...timestamps},t=>[uniqueIndex("events_slug_uq").on(t.slug),index("events_seller_status_idx").on(t.sellerId,t.status),index("events_date_idx").on(t.startsAt)]);
export const ticketTypes=sqliteTable("ticket_types",{id:text("id").primaryKey(),eventId:text("event_id").notNull().references(()=>events.id),name:text("name").notNull(),description:text("description").notNull().default(""),priceCents:integer("price_cents").notNull(),serviceFeeCents:integer("service_fee_cents").notNull(),currency:text("currency").notNull().default("usd"),inventory:integer("inventory").notNull(),sold:integer("sold").notNull().default(0),held:integer("held").notNull().default(0),minPerOrder:integer("min_per_order").notNull().default(1),maxPerOrder:integer("max_per_order").notNull().default(8),salesStart:text("sales_start"),salesEnd:text("sales_end"),status:text("status").notNull().default("active"),...timestamps},t=>[index("ticket_event_idx").on(t.eventId,t.status)]);
export const inventoryHolds=sqliteTable("inventory_holds",{id:text("id").primaryKey(),ticketTypeId:text("ticket_type_id").notNull().references(()=>ticketTypes.id),buyerUserId:text("buyer_user_id").references(()=>users.id),quantity:integer("quantity").notNull(),status:text("status").notNull().default("active"),expiresAt:text("expires_at").notNull(),idempotencyKey:text("idempotency_key").notNull(),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[uniqueIndex("holds_idempotency_uq").on(t.idempotencyKey),index("holds_expiry_idx").on(t.status,t.expiresAt)]);
export const orders=sqliteTable("orders",{id:text("id").primaryKey(),orderNumber:text("order_number").notNull(),buyerUserId:text("buyer_user_id").references(()=>users.id),sellerId:text("seller_id").notNull().references(()=>sellers.id),eventId:text("event_id").notNull().references(()=>events.id),status:text("status").notNull().default("pending_payment"),currency:text("currency").notNull().default("usd"),subtotalCents:integer("subtotal_cents").notNull(),feeCents:integer("fee_cents").notNull(),taxCents:integer("tax_cents").notNull().default(0),totalCents:integer("total_cents").notNull(),stripeCheckoutSessionId:text("stripe_checkout_session_id"),stripePaymentIntentId:text("stripe_payment_intent_id"),transferGroup:text("transfer_group"),eventSnapshot:text("event_snapshot").notNull(),policySnapshot:text("policy_snapshot").notNull(),...timestamps},t=>[uniqueIndex("orders_number_uq").on(t.orderNumber),uniqueIndex("orders_session_uq").on(t.stripeCheckoutSessionId),index("orders_buyer_idx").on(t.buyerUserId,t.createdAt),index("orders_seller_idx").on(t.sellerId,t.createdAt)]);
export const orderItems=sqliteTable("order_items",{id:text("id").primaryKey(),orderId:text("order_id").notNull().references(()=>orders.id),ticketTypeId:text("ticket_type_id").notNull().references(()=>ticketTypes.id),ticketName:text("ticket_name").notNull(),quantity:integer("quantity").notNull(),unitPriceCents:integer("unit_price_cents").notNull(),unitFeeCents:integer("unit_fee_cents").notNull(),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[index("items_order_idx").on(t.orderId)]);
export const issuedTickets=sqliteTable("issued_tickets",{id:text("id").primaryKey(),orderItemId:text("order_item_id").notNull().references(()=>orderItems.id),attendeeName:text("attendee_name"),tokenHash:text("token_hash").notNull(),status:text("status").notNull().default("issued"),transferredToUserId:text("transferred_to_user_id").references(()=>users.id),checkedInAt:text("checked_in_at"),checkedInByUserId:text("checked_in_by_user_id").references(()=>users.id),...timestamps},t=>[uniqueIndex("tickets_token_uq").on(t.tokenHash),index("tickets_item_idx").on(t.orderItemId)]);
export const ledgerEntries=sqliteTable("ledger_entries",{id:text("id").primaryKey(),sellerId:text("seller_id").references(()=>sellers.id),orderId:text("order_id").references(()=>orders.id),entryType:text("entry_type").notNull(),account:text("account").notNull(),direction:text("direction").notNull(),amountCents:integer("amount_cents").notNull(),currency:text("currency").notNull().default("usd"),providerReference:text("provider_reference"),reversalOfId:text("reversal_of_id"),idempotencyKey:text("idempotency_key").notNull(),effectiveAt:text("effective_at").notNull(),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[uniqueIndex("ledger_idempotency_uq").on(t.idempotencyKey),index("ledger_seller_idx").on(t.sellerId,t.effectiveAt),index("ledger_order_idx").on(t.orderId)]);
export const conversations=sqliteTable("conversations",{id:text("id").primaryKey(),eventId:text("event_id").notNull().references(()=>events.id),orderId:text("order_id").references(()=>orders.id),buyerUserId:text("buyer_user_id").notNull().references(()=>users.id),sellerId:text("seller_id").notNull().references(()=>sellers.id),status:text("status").notNull().default("open"),reportedAt:text("reported_at"),...timestamps},t=>[index("conversation_participants_idx").on(t.buyerUserId,t.sellerId)]);
export const messages=sqliteTable("messages",{id:text("id").primaryKey(),conversationId:text("conversation_id").notNull().references(()=>conversations.id),senderUserId:text("sender_user_id").notNull().references(()=>users.id),body:text("body").notNull(),moderationStatus:text("moderation_status").notNull().default("clear"),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[index("messages_conversation_idx").on(t.conversationId,t.createdAt)]);
export const supportCases=sqliteTable("support_cases",{id:text("id").primaryKey(),caseNumber:text("case_number").notNull(),requesterUserId:text("requester_user_id").notNull().references(()=>users.id),sellerId:text("seller_id").references(()=>sellers.id),orderId:text("order_id").references(()=>orders.id),eventId:text("event_id").references(()=>events.id),caseType:text("case_type").notNull(),category:text("category").notNull(),subject:text("subject").notNull(),priority:text("priority").notNull().default("normal"),status:text("status").notNull().default("open"),assignedAdminUserId:text("assigned_admin_user_id").references(()=>users.id),...timestamps},t=>[uniqueIndex("cases_number_uq").on(t.caseNumber),index("cases_queue_idx").on(t.caseType,t.status,t.priority)]);
export const auditLogs=sqliteTable("audit_logs",{id:text("id").primaryKey(),actorUserId:text("actor_user_id").references(()=>users.id),actorRole:text("actor_role").notNull(),action:text("action").notNull(),targetType:text("target_type").notNull(),targetId:text("target_id").notNull(),reason:text("reason"),beforeJson:text("before_json"),afterJson:text("after_json"),requestId:text("request_id"),ipHash:text("ip_hash"),success:integer("success",{mode:"boolean"}).notNull(),createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`)},t=>[index("audit_target_idx").on(t.targetType,t.targetId,t.createdAt),index("audit_actor_idx").on(t.actorUserId,t.createdAt)]);
export const processedWebhooks=sqliteTable("processed_webhooks",{eventId:text("event_id").primaryKey(),eventType:text("event_type").notNull(),processedAt:text("processed_at").notNull().default(sql`CURRENT_TIMESTAMP`)});
