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

@Entity()
@Index(["blockerId", "blockeeId"], { unique: true, where: '"groupId" IS NULL' })
@Index(["groupId", "blockeeId"], { unique: true, where: '"groupId" IS NOT NULL' })
export class CallBlocking {
	@PrimaryColumn(id())
	public id: string;

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

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

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

	@Index()
	@Column({ ...id(), nullable: true })
	public groupId: UserGroup["id"] | null;

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

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

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