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

@Entity()
@Index(["userId", "planId"], { unique: true })
export class UserPlan {
	@PrimaryColumn(id())
	public id: string;

	@Index()
	@Column("timestamp with time zone", {
		comment: "The created date of the UserPlan.",
	})
	public createdAt: Date;

	@Index()
	@Column({
		...id(),
		comment: "The user ID.",
	})
	public userId: User["id"];

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

	@Index()
	@Column({
		...id(),
		comment: "The plan ID.",
	})
	public planId: Plan["id"];

	@ManyToOne((type) => Plan, {
		onDelete: "CASCADE",
	})
	@JoinColumn()
	public plan: Plan | null;
}
