import { mysqlTable, int, varchar, tinyint, char, smallint, decimal, text, mediumtext, longtext, bigint, timestamp } from "drizzle-orm/mysql-core";

// Minimal subset needed for browsing/searching
export const entries = mysqlTable("entries", {
  id: int("id").notNull().primaryKey(),
  title: varchar("title", { length: 250 }).notNull(),
  isXrated: tinyint("is_xrated").notNull(),
  machinetypeId: tinyint("machinetype_id"),
  maxPlayers: tinyint("max_players").notNull().default(1),
  // DB allows NULLs on many of these
  languageId: char("language_id", { length: 2 }),
  genretypeId: tinyint("genretype_id"),
  genretypeSpotId: tinyint("spot_genretype_id"),
  availabletypeId: char("availabletype_id", { length: 1 }),
  withoutLoadScreen: tinyint("without_load_screen").notNull(),
  withoutInlay: tinyint("without_inlay").notNull(),
  issueId: int("issue_id"),
});

// Helper table created by ZXDB_help_search.sql
export const searchByTitles = mysqlTable("search_by_titles", {
  entryTitle: varchar("entry_title", { length: 250 }).notNull(),
  entryId: int("entry_id").notNull(),
});

export type Entry = typeof entries.$inferSelect;

// ZXDB labels (people/companies/teams)
export const labels = mysqlTable("labels", {
  id: int("id").notNull().primaryKey(),
  name: varchar("name", { length: 100 }).notNull(),
  countryId: char("country_id", { length: 2 }),
  country2Id: char("country2_id", { length: 2 }),
  fromId: int("from_id"),
  ownerId: int("owner_id"),
  wasRenamed: tinyint("was_renamed").notNull().default(0),
  deceased: varchar("deceased", { length: 200 }),
  linkWikipedia: varchar("link_wikipedia", { length: 200 }),
  linkSite: varchar("link_site", { length: 200 }),
  labeltypeId: char("labeltype_id", { length: 1 }),
});

// Helper table for names search
export const searchByNames = mysqlTable("search_by_names", {
  labelName: varchar("label_name", { length: 100 }).notNull(),
  labelId: int("label_id").notNull(),
});

// Helper: entries by authors
export const searchByAuthors = mysqlTable("search_by_authors", {
  labelId: int("label_id").notNull(),
  entryId: int("entry_id").notNull(),
});

// Helper: entries by publishers
export const searchByPublishers = mysqlTable("search_by_publishers", {
  labelId: int("label_id").notNull(),
  entryId: int("entry_id").notNull(),
});

// Relations tables
export const authors = mysqlTable("authors", {
  entryId: int("entry_id").notNull(),
  labelId: int("label_id").notNull(),
  teamId: int("team_id"),
  // Present in schema; sequence of the author for a given entry
  authorSeq: smallint("author_seq").notNull().default(1),
});

export const publishers = mysqlTable("publishers", {
  entryId: int("entry_id").notNull(),
  labelId: int("label_id").notNull(),
});

// Lookups
export const languages = mysqlTable("languages", {
  id: char("id", { length: 2 }).notNull().primaryKey(),
  // Column name in DB is `text`; map to `name` property for app ergonomics
  name: varchar("text", { length: 100 }).notNull(),
});

export const machinetypes = mysqlTable("machinetypes", {
  id: tinyint("id").notNull().primaryKey(),
  // Column name in DB is `text`
  name: varchar("text", { length: 50 }).notNull(),
});

export const genretypes = mysqlTable("genretypes", {
  id: tinyint("id").notNull().primaryKey(),
  // Column name in DB is `text`
  name: varchar("text", { length: 50 }).notNull(),
});

// Additional lookups
export const availabletypes = mysqlTable("availabletypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  // DB column `text`
  name: varchar("text", { length: 50 }).notNull(),
});

export const currencies = mysqlTable("currencies", {
  id: char("id", { length: 3 }).notNull().primaryKey(),
  name: varchar("name", { length: 50 }).notNull(),
  symbol: varchar("symbol", { length: 20 }),
  // Stored as tinyint(1) 0/1
  prefix: tinyint("prefix").notNull(),
});

// ----- Files and Filetypes (for downloads/assets) -----
export const filetypes = mysqlTable("filetypes", {
  id: tinyint("id").notNull().primaryKey(),
  // Column name in DB is `text`
  name: varchar("text", { length: 50 }).notNull(),
});

export const files = mysqlTable("files", {
  id: int("id").notNull().primaryKey(),
  labelId: int("label_id"),
  issueId: int("issue_id"),
  toolId: int("tool_id"),
  fileLink: varchar("file_link", { length: 250 }).notNull(),
  fileDate: varchar("file_date", { length: 50 }),
  fileSize: int("file_size"),
  fileMd5: varchar("file_md5", { length: 32 }),
  filetypeId: tinyint("filetype_id").notNull(),
  comments: varchar("comments", { length: 250 }),
});

export const schemetypes = mysqlTable("schemetypes", {
  id: char("id", { length: 2 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const sourcetypes = mysqlTable("sourcetypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const casetypes = mysqlTable("casetypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const roletypes = mysqlTable("roletypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const hosts = mysqlTable("hosts", {
  id: tinyint("id").notNull().primaryKey(),
  title: varchar("title", { length: 150 }).notNull(),
  link: varchar("link", { length: 150 }).notNull(),
  admin: varchar("admin", { length: 150 }).notNull(),
  magazineId: smallint("magazine_id"),
});

// ---- Magazines and Issues (subset used by the app) ----
export const magazines = mysqlTable("magazines", {
  id: smallint("id").notNull().primaryKey(),
  // ZXDB column is `name`
  name: varchar("name", { length: 100 }).notNull(),
  countryId: char("country_id", { length: 2 }).notNull(),
  languageId: char("language_id", { length: 2 }).notNull(),
  linkSite: varchar("link_site", { length: 200 }),
  magtypeId: char("magtype_id", { length: 1 }).notNull(),
  topicId: int("topic_id"),
  linkMask: varchar("link_mask", { length: 250 }),
  archiveMask: varchar("archive_mask", { length: 250 }),
  translationMask: varchar("translation_mask", { length: 250 }),
});

export const issues = mysqlTable("issues", {
  id: int("id").notNull().primaryKey(),
  magazineId: smallint("magazine_id").notNull(),
  dateYear: smallint("date_year"),
  dateMonth: smallint("date_month"),
  dateDay: smallint("date_day"),
  volume: smallint("volume"),
  number: smallint("number"),
  special: varchar("special", { length: 100 }),
  supplement: varchar("supplement", { length: 100 }),
  linkMask: varchar("link_mask", { length: 250 }),
  archiveMask: varchar("archive_mask", { length: 250 }),
});

// ---- Aliases (alternative titles per entry/release/language)
export const aliases = mysqlTable("aliases", {
  entryId: int("entry_id").notNull(),
  releaseSeq: smallint("release_seq").notNull().default(0),
  languageId: char("language_id", { length: 2 }).notNull(),
  title: varchar("title", { length: 250 }).notNull(),
});

// `releases` are identified by (entry_id, release_seq)
export const releases = mysqlTable("releases", {
  entryId: int("entry_id").notNull(),
  releaseSeq: smallint("release_seq").notNull(),
  releaseYear: smallint("release_year"),
  releaseMonth: smallint("release_month"),
  releaseDay: smallint("release_day"),
  currencyId: char("currency_id", { length: 3 }),
  releasePrice: decimal("release_price", { precision: 9, scale: 2 }),
  budgetPrice: decimal("budget_price", { precision: 9, scale: 2 }),
  microdrivePrice: decimal("microdrive_price", { precision: 9, scale: 2 }),
  diskPrice: decimal("disk_price", { precision: 9, scale: 2 }),
  cartridgePrice: decimal("cartridge_price", { precision: 9, scale: 2 }),
  bookIsbn: varchar("book_isbn", { length: 50 }),
  bookPages: smallint("book_pages"),
});

// Downloads are linked to a release via (entry_id, release_seq)
export const downloads = mysqlTable("downloads", {
  id: int("id").notNull().primaryKey(),
  entryId: int("entry_id").notNull(),
  releaseSeq: smallint("release_seq").notNull().default(0),
  fileLink: varchar("file_link", { length: 250 }).notNull(),
  fileDate: varchar("file_date", { length: 50 }),
  fileSize: int("file_size"),
  fileMd5: varchar("file_md5", { length: 32 }),
  filetypeId: tinyint("filetype_id").notNull(),
  scrBorder: tinyint("scr_border").notNull().default(7),
  languageId: char("language_id", { length: 2 }),
  isDemo: tinyint("is_demo").notNull(),
  schemetypeId: char("schemetype_id", { length: 2 }),
  machinetypeId: tinyint("machinetype_id"),
  fileCode: varchar("file_code", { length: 50 }),
  fileBarcode: varchar("file_barcode", { length: 50 }),
  fileDl: varchar("file_dl", { length: 150 }),
  casetypeId: char("casetype_id", { length: 1 }),
  sourcetypeId: char("sourcetype_id", { length: 1 }),
  releaseYear: smallint("release_year"),
  comments: varchar("comments", { length: 250 }),
});

// ---- Web references (external links tied to entries)
export const webrefs = mysqlTable("webrefs", {
  entryId: int("entry_id").notNull(),
  link: varchar("link", { length: 200 }).notNull(),
  websiteId: tinyint("website_id").notNull(),
  languageId: char("language_id", { length: 2 }).notNull(),
});

export const websites = mysqlTable("websites", {
  id: tinyint("id").notNull().primaryKey(),
  name: varchar("name", { length: 100 }).notNull(),
  comments: varchar("comments", { length: 100 }),
  link: varchar("link", { length: 100 }),
  linkMask: varchar("link_mask", { length: 100 }),
});

// Roles relation (composite PK in DB)
export const roles = mysqlTable("roles", {
  entryId: int("entry_id").notNull(),
  labelId: int("label_id").notNull(),
  roletypeId: char("roletype_id", { length: 1 }).notNull(),
});

// ---- Additional ZXDB schema coverage (lookups and content) ----

export const articletypes = mysqlTable("articletypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const articles = mysqlTable("articles", {
  labelId: int("label_id").notNull(),
  link: varchar("link", { length: 200 }).notNull(),
  articletypeId: char("articletype_id", { length: 1 }).notNull(),
  title: varchar("title", { length: 200 }),
  languageId: char("language_id", { length: 2 }).notNull(),
  writer: varchar("writer", { length: 200 }),
  dateYear: smallint("date_year"),
});

export const categories = mysqlTable("categories", {
  id: smallint("id").notNull().primaryKey(),
  // DB column `text`
  name: varchar("text", { length: 50 }).notNull(),
});

export const contenttypes = mysqlTable("contenttypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const contents = mysqlTable("contents", {
  // ZXDB contents table does not have its own `id`; natural key is (issue_id, page_from, page_to, label_id, entry_id)
  entryId: int("entry_id").notNull(),
  labelId: int("label_id"),
  issueId: int("issue_id").notNull(),
  contenttypeId: char("contenttype_id", { length: 1 }).notNull(),
  pageFrom: smallint("page_from"),
  pageTo: smallint("page_to"),
  title: varchar("title", { length: 200 }),
  dateYear: smallint("date_year"),
  rating: tinyint("rating"),
  comments: varchar("comments", { length: 250 }),
});

export const extensions = mysqlTable("extensions", {
  ext: varchar("ext", { length: 15 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const features = mysqlTable("features", {
  id: int("id").notNull().primaryKey(),
  name: varchar("name", { length: 150 }).notNull(),
  version: tinyint("version").notNull().default(0),
  labelId: int("label_id"),
  label2Id: int("label2_id"),
});

export const tooltypes = mysqlTable("tooltypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const tools = mysqlTable("tools", {
  id: int("id").notNull().primaryKey(),
  title: varchar("title", { length: 200 }).notNull(),
  languageId: char("language_id", { length: 2 }),
  tooltypeId: char("tooltype_id", { length: 1 }),
  link: varchar("link", { length: 200 }),
});

// ---- Magazine references (per-issue references to entries/labels/topics) ----
export const referencetypes = mysqlTable("referencetypes", {
  id: tinyint("id").notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const magrefs = mysqlTable("magrefs", {
  id: int("id").notNull().primaryKey(),
  referencetypeId: tinyint("referencetype_id").notNull(),
  entryId: int("entry_id"),
  labelId: int("label_id"),
  topicId: int("topic_id"),
  issueId: int("issue_id").notNull(),
  page: smallint("page").notNull().default(0),
  isOriginal: tinyint("is_original").notNull().default(0),
  scoreGroup: varchar("score_group", { length: 100 }).notNull().default(""),
  reviewId: int("review_id"),
  awardId: tinyint("award_id"),
});

// ---- Extended ZXDB schema coverage (structure-only) ----

export const booktypeins = mysqlTable("booktypeins", {
  entryId: int("entry_id").notNull(),
  bookId: int("book_id").notNull(),
  installment: smallint("installment").notNull().default(0),
  volume: smallint("volume").notNull().default(0),
  page: smallint("page").notNull().default(0),
  isOriginal: tinyint("is_original").notNull().default(0),
});

export const countries = mysqlTable("countries", {
  id: char("id", { length: 2 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const labeltypes = mysqlTable("labeltypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const licenses = mysqlTable("licenses", {
  id: int("id").notNull().primaryKey(),
  name: varchar("name", { length: 100 }).notNull(),
  licensetypeId: char("licensetype_id", { length: 1 }).notNull(),
  linkWikipedia: varchar("link_wikipedia", { length: 200 }),
  linkSite: varchar("link_site", { length: 200 }),
  comments: varchar("comments", { length: 500 }),
});

export const licensetypes = mysqlTable("licensetypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const licensors = mysqlTable("licensors", {
  licenseId: int("license_id").notNull(),
  labelId: int("label_id").notNull(),
});

export const magreffeats = mysqlTable("magreffeats", {
  magrefId: int("magref_id").notNull(),
  featureId: int("feature_id").notNull(),
});

export const magreflinks = mysqlTable("magreflinks", {
  magrefId: int("magref_id").notNull(),
  link: varchar("link", { length: 250 }).notNull(),
  hostId: tinyint("host_id").notNull(),
});

export const magtypes = mysqlTable("magtypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const members = mysqlTable("members", {
  tagId: int("tag_id").notNull(),
  entryId: int("entry_id").notNull(),
  categoryId: smallint("category_id").notNull().default(1),
  memberSeq: smallint("member_seq"),
});

export const notes = mysqlTable("notes", {
  id: int("id").notNull().primaryKey(),
  entryId: int("entry_id"),
  labelId: int("label_id"),
  notetypeId: char("notetype_id", { length: 1 }).notNull(),
  text: mediumtext("text").notNull(),
});

export const notetypes = mysqlTable("notetypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 100 }).notNull(),
});

export const nvgs = mysqlTable("nvgs", {
  id: int("id").notNull().primaryKey(),
  title: varchar("title", { length: 250 }).notNull(),
  entryId: int("entry_id"),
  fileLink: varchar("file_link", { length: 250 }),
  fileDate: varchar("file_date", { length: 50 }),
  fileSize: int("file_size"),
  filetypeId: tinyint("filetype_id"),
  isDemo: tinyint("is_demo").notNull().default(0),
  machinetypeId: tinyint("machinetype_id"),
  comments: varchar("comments", { length: 500 }),
  url: varchar("url", { length: 100 }),
});

export const origintypes = mysqlTable("origintypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const permissions = mysqlTable("permissions", {
  websiteId: tinyint("website_id").notNull(),
  labelId: int("label_id").notNull(),
  permissiontypeId: char("permissiontype_id", { length: 1 }).notNull(),
  text: varchar("text", { length: 300 }),
});

export const permissiontypes = mysqlTable("permissiontypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const platforms = mysqlTable("platforms", {
  id: tinyint("id").notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const ports = mysqlTable("ports", {
  id: int("id").notNull().primaryKey(),
  title: varchar("title", { length: 250 }),
  entryId: int("entry_id").notNull(),
  platformId: tinyint("platform_id").notNull(),
  isOfficial: tinyint("is_official").notNull(),
  linkSystem: varchar("link_system", { length: 200 }),
});

export const prefixes = mysqlTable("prefixes", {
  text: varchar("text", { length: 10 }).notNull().primaryKey(),
});

export const prefixexempts = mysqlTable("prefixexempts", {
  text: varchar("text", { length: 50 }).notNull().primaryKey(),
});

export const relatedlicenses = mysqlTable("relatedlicenses", {
  entryId: int("entry_id").notNull(),
  licenseId: int("license_id").notNull(),
  isOfficial: tinyint("is_official").notNull(),
});

export const relations = mysqlTable("relations", {
  entryId: int("entry_id").notNull(),
  originalId: int("original_id").notNull(),
  relationtypeId: char("relationtype_id", { length: 1 }).notNull(),
});

export const relationtypes = mysqlTable("relationtypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
  reciprocal: varchar("reciprocal", { length: 50 }).notNull(),
  comments: varchar("comments", { length: 250 }),
});

export const remakes = mysqlTable("remakes", {
  id: int("id").notNull().primaryKey(),
  entryId: int("entry_id").notNull(),
  title: varchar("title", { length: 250 }).notNull(),
  fileLink: varchar("file_link", { length: 250 }).notNull(),
  fileDate: varchar("file_date", { length: 50 }),
  fileSize: int("file_size"),
  authors: varchar("authors", { length: 250 }),
  platforms: varchar("platforms", { length: 200 }),
  remakeYears: varchar("remake_years", { length: 100 }),
  remakeStatus: varchar("remake_status", { length: 1000 }),
});

export const scores = mysqlTable("scores", {
  websiteId: tinyint("website_id").notNull(),
  entryId: int("entry_id").notNull(),
  score: decimal("score", { precision: 5, scale: 2 }).notNull(),
  votes: int("votes").notNull(),
});

export const scraps = mysqlTable("scraps", {
  id: int("id").notNull().primaryKey(),
  entryId: int("entry_id"),
  releaseSeq: smallint("release_seq"),
  fileLink: varchar("file_link", { length: 250 }),
  fileDate: varchar("file_date", { length: 50 }),
  fileSize: int("file_size"),
  filetypeId: tinyint("filetype_id").notNull(),
  languageId: char("language_id", { length: 2 }),
  isDemo: tinyint("is_demo").notNull(),
  schemetypeId: char("schemetype_id", { length: 2 }),
  machinetypeId: tinyint("machinetype_id"),
  fileCode: varchar("file_code", { length: 50 }),
  fileBarcode: varchar("file_barcode", { length: 50 }),
  fileDl: varchar("file_dl", { length: 150 }),
  casetypeId: char("casetype_id", { length: 1 }),
  sourcetypeId: char("sourcetype_id", { length: 1 }),
  releaseYear: smallint("release_year"),
  comments: varchar("comments", { length: 250 }),
  rationale: varchar("rationale", { length: 100 }).notNull(),
});

export const searchByAliases = mysqlTable("search_by_aliases", {
  entryId: int("entry_id").notNull(),
  title: varchar("title", { length: 250 }).notNull(),
  libraryTitle: varchar("library_title", { length: 300 }).notNull(),
});

export const searchByIssues = mysqlTable("search_by_issues", {
  issueId: int("issue_id").notNull().primaryKey(),
  name: varchar("name", { length: 300 }).notNull(),
});

export const searchByMagazines = mysqlTable("search_by_magazines", {
  magazineId: smallint("magazine_id").notNull(),
  labelId: int("label_id").notNull(),
});

export const searchByMagrefs = mysqlTable("search_by_magrefs", {
  entryId: int("entry_id").notNull(),
  magrefId: int("magref_id").notNull(),
});

export const searchByOrigins = mysqlTable("search_by_origins", {
  entryId: int("entry_id").notNull().primaryKey(),
  libraryTitle: varchar("library_title", { length: 300 }).notNull(),
  origintypeId: char("origintype_id", { length: 1 }).notNull(),
  containerId: int("container_id"),
  issueId: int("issue_id"),
  dateYear: smallint("date_year"),
  dateMonth: smallint("date_month"),
  dateDay: smallint("date_day"),
  publication: varchar("publication", { length: 300 }),
});

export const spexAuthors = mysqlTable("spex_authors", {
  id: int("id").notNull().primaryKey(),
  entryId: int("entry_id").notNull(),
  name: varchar("name", { length: 150 }).notNull(),
  labelId: int("label_id"),
});

export const spexEntries = mysqlTable("spex_entries", {
  id: int("id").notNull().primaryKey(),
  title: varchar("title", { length: 150 }).notNull(),
  entryId: int("entry_id").notNull(),
  releaseSeq: smallint("release_seq"),
  pub1LabelId: int("pub1_label_id"),
  pub2LabelId: int("pub2_label_id"),
  pub3LabelId: int("pub3_label_id"),
  genretypeId: tinyint("genretype_id"),
  orgprice: decimal("orgprice", { precision: 5, scale: 2 }).notNull(),
  repub2price: decimal("repub2price", { precision: 5, scale: 2 }).notNull(),
  repub3price: decimal("repub3price", { precision: 5, scale: 2 }).notNull(),
  diskprice: decimal("diskprice", { precision: 5, scale: 2 }).notNull(),
  fgtkey: varchar("fgtkey", { length: 150 }).notNull(),
});

export const tags = mysqlTable("tags", {
  id: int("id").notNull().primaryKey(),
  name: varchar("name", { length: 100 }).notNull(),
  link: varchar("link", { length: 200 }),
  comments: varchar("comments", { length: 1500 }),
  tagtypeId: char("tagtype_id", { length: 1 }).notNull(),
  toolId: int("tool_id"),
});

export const tagtypes = mysqlTable("tagtypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const topics = mysqlTable("topics", {
  id: int("id").notNull().primaryKey(),
  topictypeId: char("topictype_id", { length: 1 }).notNull(),
  labelId: int("label_id"),
  magazineId: smallint("magazine_id"),
  name: varchar("name", { length: 150 }).notNull(),
  comments: varchar("comments", { length: 150 }),
});

export const topictypes = mysqlTable("topictypes", {
  id: char("id", { length: 1 }).notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
});

export const zxsrAwards = mysqlTable("zxsr_awards", {
  id: tinyint("id").notNull().primaryKey(),
  name: varchar("text", { length: 50 }).notNull(),
  magazineId: smallint("magazine_id").notNull(),
});

export const zxsrCaptions = mysqlTable("zxsr_captions", {
  magrefId: int("magref_id").notNull(),
  captionSeq: smallint("caption_seq").notNull(),
  text: text("text").notNull(),
  isBanner: tinyint("is_banner").notNull(),
});

export const zxsrReviews = mysqlTable("zxsr_reviews", {
  id: int("id").notNull().primaryKey(),
  introText: longtext("intro_text"),
  reviewText: longtext("review_text"),
  reviewRating: varchar("review_rating", { length: 2000 }),
});

export const zxsrScores = mysqlTable("zxsr_scores", {
  id: int("id").notNull().primaryKey(),
  magrefId: int("magref_id").notNull(),
  scoreSeq: tinyint("score_seq"),
  category: varchar("category", { length: 100 }).notNull(),
  isOverall: tinyint("is_overall").notNull().default(0),
  score: varchar("score", { length: 100 }),
  comments: text("comments"),
});

// ---- Derived tables (managed by update scripts, not part of ZXDB upstream) ----

// Stores MD5, CRC32 and size of the inner tape file extracted from download zips.
// Populated by bin/update-software-hashes.mjs; survives DB wipes via JSON snapshot.
export const softwareHashes = mysqlTable("software_hashes", {
  downloadId: int("download_id").notNull().primaryKey(),
  md5: varchar("md5", { length: 32 }).notNull(),
  crc32: varchar("crc32", { length: 8 }).notNull(),
  sizeBytes: bigint("size_bytes", { mode: "number" }).notNull(),
  innerPath: varchar("inner_path", { length: 500 }).notNull(),
  updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
