import { Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { Page } from "./page.entity";

// "faq-selection" holds a JSON array of FaqItem ids (see
// src/faq/entities/faq-item.entity.ts), in display order — picked via the
// admin's FaqSelector instead of typed inline, so the same FAQ can be
// reused across pages. Column is varchar(20), same as the other types.
export type ContentBlockType = "text" | "richtext" | "image" | "faq-selection";

@Entity("content_blocks")
export class ContentBlock {
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  // @JoinColumn's name must match the @Column below exactly — that's what
  // makes TypeORM treat them as the same underlying "page_id" column
  // instead of creating a second implicit FK column.
  @ManyToOne(() => Page, (page) => page.blocks, { onDelete: "CASCADE" })
  @JoinColumn({ name: "page_id" })
  page!: Page;

  @Column({ name: "page_id" })
  pageId!: string;

  // Stable identifier a page component looks itself up by, e.g.
  // "hero.headline", "hero.stat1.value" — unique per page, not globally.
  @Column()
  key!: string;

  // A human label for the admin editor ("Hero Headline") — the `key` above
  // is for code, this is for the person editing it.
  @Column()
  label!: string;

  @Column({ type: "varchar", length: 20, default: "text" })
  type!: ContentBlockType;

  @Column({ name: "text_value", type: "longtext", nullable: true })
  textValue!: string | null;

  @Column({ name: "image_url", type: "varchar", length: 500, nullable: true })
  imageUrl!: string | null;

  @Column({ name: "sort_order", default: 0 })
  sortOrder!: number;

  @CreateDateColumn({ name: "created_at" })
  createdAt!: Date;

  @UpdateDateColumn({ name: "updated_at" })
  updatedAt!: Date;
}
