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

@Entity("shogi_game")
export class ShogiGame {
	@PrimaryColumn(id())
	public id: string;

	@Column("timestamp with time zone", { nullable: true })
	public startedAt: Date | null;

	@Column("timestamp with time zone", { nullable: true })
	public endedAt: Date | null;

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

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

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

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

	@Column("boolean", { default: false })
	public user1Ready: boolean;

	@Column("boolean", { default: false })
	public user2Ready: boolean;

	@Column("integer", { nullable: true })
	public sente: number | null;

	@Column("boolean", { default: false })
	public isStarted: boolean;

	@Column("boolean", { default: false })
	public isEnded: boolean;

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

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

	@Column("text")
	public sfen: string;

	@Column("jsonb", { default: [] })
	public logs: Array<{
		id: string | null;
		at: number;
		userId: User["id"];
		usi: string;
		sfen: string;
	}>;
}
