import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const learnerState = sqliteTable("learner_state", {
  email: text("email").primaryKey(),
  displayName: text("display_name").notNull(),
  pathway: text("pathway").notNull().default("Project management"),
  projectStarted: integer("project_started", { mode: "boolean" }).notNull().default(false),
  completedLessons: integer("completed_lessons").notNull().default(3),
  portfolioPublished: integer("portfolio_published", { mode: "boolean" }).notNull().default(false),
  organisationName: text("organisation_name").notNull().default("ConnectPoint Learning"),
  accentColour: text("accent_colour").notNull().default("#08a9df"),
  organisationLogoKey: text("organisation_logo_key"),
  organisationLogoName: text("organisation_logo_name"),
  activeRole: text("active_role").notNull().default("learner"),
  updatedAt: text("updated_at").notNull(),
});

export const evidence = sqliteTable("evidence", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  ownerEmail: text("owner_email").notNull(),
  title: text("title").notNull(),
  skill: text("skill").notNull(),
  status: text("status").notNull().default("growing"),
  createdAt: text("created_at").notNull(),
});

export const organisationMembers = sqliteTable("organisation_members", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  email: text("email").notNull().unique(),
  displayName: text("display_name"),
  role: text("role").notNull().default("learner"),
  status: text("status").notNull().default("invited"),
  invitedBy: text("invited_by").notNull(),
  createdAt: text("created_at").notNull(),
  updatedAt: text("updated_at").notNull(),
});

export const platformSettings = sqliteTable("platform_settings", {
  key: text("key").primaryKey(),
  value: text("value").notNull(),
  updatedAt: text("updated_at").notNull(),
});

export const programmes = sqliteTable("programmes", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  ownerEmail: text("owner_email").notNull(),
  title: text("title").notNull(),
  audience: text("audience").notNull(),
  skill: text("skill").notNull(),
  status: text("status").notNull().default("draft"),
  createdAt: text("created_at").notNull(),
});

export const assessmentAttempts = sqliteTable("assessment_attempts", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  learnerEmail: text("learner_email").notNull(),
  programmeId: integer("programme_id").notNull(),
  answer: text("answer").notNull(),
  score: integer("score").notNull(),
  feedback: text("feedback").notNull(),
  createdAt: text("created_at").notNull(),
});

export const projectSubmissions = sqliteTable("project_submissions", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  learnerEmail: text("learner_email").notNull(),
  projectTitle: text("project_title").notNull(),
  reflection: text("reflection").notNull(),
  fileKey: text("file_key"),
  fileName: text("file_name"),
  contentType: text("content_type"),
  status: text("status").notNull().default("submitted"),
  score: integer("score"),
  rubricFeedback: text("rubric_feedback"),
  cvStatement: text("cv_statement"),
  createdAt: text("created_at").notNull(),
  assessedAt: text("assessed_at"),
});

export const courseProducts = sqliteTable("course_products", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  creatorEmail: text("creator_email").notNull(),
  title: text("title").notNull(),
  description: text("description").notNull(),
  category: text("category").notNull(),
  outcome: text("outcome").notNull(),
  projectTitle: text("project_title").notNull(),
  lessonCount: integer("lesson_count").notNull().default(6),
  pricePence: integer("price_pence").notNull(),
  status: text("status").notNull().default("published"),
  createdAt: text("created_at").notNull(),
});

export const enrolments = sqliteTable("enrolments", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  learnerEmail: text("learner_email").notNull(),
  productId: integer("product_id").notNull(),
  amountPence: integer("amount_pence").notNull(),
  paymentStatus: text("payment_status").notNull().default("demo"),
  progress: integer("progress").notNull().default(0),
  enrolledAt: text("enrolled_at").notNull(),
});

export const courseModules = sqliteTable("course_modules", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  productId: integer("product_id").notNull(),
  title: text("title").notNull(),
  position: integer("position").notNull(),
});

export const courseLessons = sqliteTable("course_lessons", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  moduleId: integer("module_id").notNull(),
  title: text("title").notNull(),
  lessonType: text("lesson_type").notNull().default("video"),
  summary: text("summary").notNull(),
  mediaKey: text("media_key"),
  mediaName: text("media_name"),
  mediaUrl: text("media_url"),
  durationMinutes: integer("duration_minutes").notNull().default(5),
  position: integer("position").notNull(),
  status: text("status").notNull().default("draft"),
  createdAt: text("created_at").notNull(),
});

export const trainingContent = sqliteTable("training_content", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  ownerEmail: text("owner_email").notNull(),
  title: text("title").notNull(),
  format: text("format").notNull(),
  launchUrl: text("launch_url"),
  packageKey: text("package_key"),
  packageName: text("package_name"),
  launchPath: text("launch_path"),
  status: text("status").notNull().default("ready"),
  createdAt: text("created_at").notNull(),
});

export const contentLaunches = sqliteTable("content_launches", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  contentId: integer("content_id").notNull(),
  learnerEmail: text("learner_email").notNull(),
  completionStatus: text("completion_status").notNull().default("launched"),
  score: integer("score"),
  totalSeconds: integer("total_seconds").notNull().default(0),
  launchedAt: text("launched_at").notNull(),
  updatedAt: text("updated_at").notNull(),
});

export const surveyResponses = sqliteTable("survey_responses", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  learnerEmail: text("learner_email").notNull(),
  courseTitle: text("course_title").notNull(),
  rating: integer("rating").notNull(),
  confidence: integer("confidence").notNull(),
  comment: text("comment").notNull().default(""),
  createdAt: text("created_at").notNull(),
});
