import { sql } from "drizzle-orm";
import { integer, real, sqliteTable, text, uniqueIndex, index } from "drizzle-orm/sqlite-core";
const ts = { createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`), updatedAt: text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP`) };
export const admins = sqliteTable("admins", { id:text("id").primaryKey(), email:text("email").notNull(), name:text("name").notNull(), passwordHash:text("password_hash").notNull(), salt:text("salt").notNull(), role:text("role").notNull().default("administrator"), active:integer("active",{mode:"boolean"}).notNull().default(true), lastLoginAt:text("last_login_at"), ...ts }, t=>[uniqueIndex("admins_email_unique").on(t.email)]);
export const adminSessions = sqliteTable("admin_sessions", { id:text("id").primaryKey(), adminId:text("admin_id").notNull().references(()=>admins.id,{onDelete:"cascade"}), tokenHash:text("token_hash").notNull(), expiresAt:text("expires_at").notNull(), createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`) }, t=>[uniqueIndex("admin_sessions_token_unique").on(t.tokenHash),index("idx_sessions_admin").on(t.adminId)]);
export const events = sqliteTable("events", { id:text("id").primaryKey(), slug:text("slug").notNull(), name:text("name").notNull(), theme:text("theme").notNull(), description:text("description").notNull(), startsAt:text("starts_at").notNull(), endsAt:text("ends_at").notNull(), venueName:text("venue_name").notNull(), venueAddress:text("venue_address").notNull(), currency:text("currency").notNull().default("USD"), registrationFee:real("registration_fee").notNull().default(0), registrationOpen:integer("registration_open",{mode:"boolean"}).notNull().default(true), registrationDeadline:text("registration_deadline"), contactEmail:text("contact_email").notNull(), contactPhone:text("contact_phone"), heroImage:text("hero_image"), registrationPrefix:text("registration_prefix").notNull().default("OFM-CONF"), supportAmounts:text("support_amounts").notNull().default("50,100,250,500,1000"), ...ts },t=>[uniqueIndex("events_slug_unique").on(t.slug)]);
export const registrations = sqliteTable("registrations", { id:text("id").primaryKey(), eventId:text("event_id").notNull().references(()=>events.id), registrationNumber:text("registration_number").notNull(), firstName:text("first_name").notNull(), lastName:text("last_name").notNull(), email:text("email").notNull(), phone:text("phone").notNull(), country:text("country").notNull(), city:text("city").notNull(), state:text("state"), gender:text("gender"), ageRange:text("age_range"), church:text("church"), role:text("role"), source:text("source"), attendanceType:text("attendance_type").notNull().default("in-person"), attendeeCount:integer("attendee_count").notNull().default(1), notes:text("notes"), consent:integer("consent",{mode:"boolean"}).notNull(), communicationConsent:integer("communication_consent",{mode:"boolean"}).notNull().default(false), status:text("status").notNull().default("pending"), paymentStatus:text("payment_status").notNull().default("unpaid"), amountDue:real("amount_due").notNull().default(0), ...ts },t=>[uniqueIndex("registrations_number_unique").on(t.registrationNumber),index("idx_registrations_event_status").on(t.eventId,t.status),index("idx_registrations_email").on(t.email),index("idx_registrations_payment").on(t.paymentStatus)]);
export const payments = sqliteTable("payments", { id:text("id").primaryKey(), eventId:text("event_id").notNull().references(()=>events.id), registrationId:text("registration_id").references(()=>registrations.id), supporterName:text("supporter_name"), supporterEmail:text("supporter_email"), supporterPhone:text("supporter_phone"), supporterCountry:text("supporter_country"), supporterMessage:text("supporter_message"), purpose:text("purpose").notNull(), amount:real("amount").notNull(), currency:text("currency").notNull(), status:text("status").notNull().default("pending"), provider:text("provider").notNull().default("stripe"), providerReference:text("provider_reference"), checkoutSessionId:text("checkout_session_id"), failureReason:text("failure_reason"), refundedAt:text("refunded_at"), ...ts },t=>[uniqueIndex("payments_provider_ref_unique").on(t.providerReference),index("idx_payments_status_purpose").on(t.status,t.purpose),index("idx_payments_registration").on(t.registrationId)]);
export const communications = sqliteTable("communications", { id:text("id").primaryKey(), registrationId:text("registration_id").references(()=>registrations.id), channel:text("channel").notNull(), recipient:text("recipient").notNull(), subject:text("subject"), body:text("body").notNull(), templateKey:text("template_key"), providerMessageId:text("provider_message_id"), status:text("status").notNull().default("queued"), error:text("error"), idempotencyKey:text("idempotency_key"), sentAt:text("sent_at"), ...ts },t=>[uniqueIndex("communications_idempotency_unique").on(t.idempotencyKey),index("idx_comms_registration").on(t.registrationId),index("idx_comms_channel_status").on(t.channel,t.status)]);
export const speakers = sqliteTable("speakers", { id:text("id").primaryKey(), eventId:text("event_id").notNull().references(()=>events.id), name:text("name").notNull(), title:text("title").notNull(), biography:text("biography").notNull(), imageUrl:text("image_url"), socialUrl:text("social_url"), sortOrder:integer("sort_order").notNull().default(0), ...ts });
export const scheduleItems = sqliteTable("schedule_items", { id:text("id").primaryKey(), eventId:text("event_id").notNull().references(()=>events.id), startsAt:text("starts_at").notNull(), endsAt:text("ends_at"), title:text("title").notNull(), speaker:text("speaker"), description:text("description"), location:text("location"), sortOrder:integer("sort_order").notNull().default(0), ...ts });
export const faqItems = sqliteTable("faq_items", { id:text("id").primaryKey(), eventId:text("event_id").notNull().references(()=>events.id), question:text("question").notNull(), answer:text("answer").notNull(), sortOrder:integer("sort_order").notNull().default(0), active:integer("active",{mode:"boolean"}).notNull().default(true), ...ts });
export const adminNotes = sqliteTable("admin_notes", { id:text("id").primaryKey(), registrationId:text("registration_id").notNull().references(()=>registrations.id,{onDelete:"cascade"}), adminId:text("admin_id").notNull().references(()=>admins.id), note:text("note").notNull(), createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`) });
export const webhookEvents = sqliteTable("webhook_events", { id:text("id").primaryKey(), provider:text("provider").notNull(), eventType:text("event_type").notNull(), processedAt:text("processed_at").notNull().default(sql`CURRENT_TIMESTAMP`) },t=>[uniqueIndex("webhook_events_provider_id_unique").on(t.provider,t.id)]);
export const activityLogs = sqliteTable("activity_logs", { id:text("id").primaryKey(), registrationId:text("registration_id").references(()=>registrations.id), adminId:text("admin_id").references(()=>admins.id), action:text("action").notNull(), detail:text("detail"), createdAt:text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`) },t=>[index("idx_activity_registration").on(t.registrationId)]);
