import {
	Column,
	Entity,
	Index,
	JoinColumn,
	ManyToOne,
	PrimaryColumn,
} from "typeorm";
import { id } from "../id.js";
import { User } from "./user.js";
import { Note } from "./note.js";

export type ScheduledNoteStatus = "scheduled" | "processing" | "published" | "failed";

@Entity("scheduled_note")
export class ScheduledNote {
	@PrimaryColumn(id())
	public id: string;

	@Index()
	@Column("timestamp with time zone")
	public createdAt: Date;

	@Index()
	@Column("timestamp with time zone")
	public scheduledAt: Date;

	@Index()
	@Column(id())
	public userId: User["id"];

	@ManyToOne(() => User, { onDelete: "CASCADE" })
	@JoinColumn()
	public user: User | null;

	@Column("varchar", { length: 16, default: "scheduled" })
	public status: ScheduledNoteStatus;

	@Column("jsonb")
	public data: Record<string, any>;

	@Column({ ...id(), nullable: true })
	public noteId: Note["id"] | null;

	@ManyToOne(() => Note, { onDelete: "SET NULL" })
	@JoinColumn()
	public note: Note | null;

	@Column("text", { nullable: true })
	public error: string | null;
}
