import { DataTypes } from 'sequelize';
import { env } from '$env/dynamic/private';
import { building, dev } from '$app/environment';
import * as bcrypt from 'bcrypt';
import {
	BeforeCreate,
	BeforeUpdate,
	BelongsTo,
	Column,
	ForeignKey,
	Index,
	Model,
	Sequelize,
	Table
} from 'sequelize-typescript';
import { Permissions } from '$lib/permissions';

@Table({ modelName: 'user', underscored: true })
export class User extends Model {
	@Column({ type: DataTypes.STRING, allowNull: false })
	declare firstname: string;
	@Column({ type: DataTypes.STRING, allowNull: false })
	declare lastname: string;
	@Column({ type: DataTypes.DATE, allowNull: false })
	declare birthday: Date;
	@Column({ type: DataTypes.STRING })
	declare telephone: string;
	@Column({ type: DataTypes.STRING, allowNull: false })
	declare username: string;
	@Column({ type: DataTypes.ENUM('java', 'bedrock', 'cracked'), allowNull: false })
	declare playertype: 'java' | 'bedrock' | 'cracked';
	@Column({ type: DataTypes.STRING })
	declare password: string;
	@Column({ type: DataTypes.UUIDV4 })
	@Index
	declare uuid: string;
}

@Table({ modelName: 'report', underscored: true })
export class Report extends Model {
	@Column({ type: DataTypes.STRING, allowNull: false, unique: true })
	@Index
	declare url_hash: string;
	@Column({ type: DataTypes.STRING, allowNull: false })
	declare subject: string;
	@Column({ type: DataTypes.STRING })
	declare body: string;
	@Column({ type: DataTypes.BOOLEAN, allowNull: false })
	declare draft: boolean;
	@Column({ type: DataTypes.ENUM('java', 'bedrock', 'cracked'), allowNull: false })
	declare status: 'none' | 'review' | 'reviewed';
	@Column({ type: DataTypes.STRING })
	declare notice: string;
	@Column({ type: DataTypes.STRING })
	declare statement: string;
	@Column({ type: DataTypes.INTEGER, allowNull: false })
	@ForeignKey(() => User)
	declare reporter_id: number;
	@Column({ type: DataTypes.INTEGER, allowNull: false })
	@ForeignKey(() => User)
	declare reported_id: number;
	@Column({ type: DataTypes.INTEGER })
	@ForeignKey(() => Admin)
	declare auditor_id: number;

	@BelongsTo(() => User, 'reporter_id')
	declare reporter: User;
	@BelongsTo(() => User, 'reported_id')
	declare reported: User;
	@BelongsTo(() => Admin, 'auditor_id')
	declare auditor: Admin;
}

@Table({ modelName: 'admin', underscored: true })
export class Admin extends Model {
	@Column({ type: DataTypes.STRING, allowNull: false, unique: true })
	declare username: string;
	@Column({ type: DataTypes.STRING, allowNull: false })
	declare password: string;
	@Column({
		type: DataTypes.BIGINT,
		allowNull: false,
		get(this: Admin): Permissions | null {
			const permissions = this.getDataValue('permissions');
			return permissions != null ? new Permissions(permissions) : null;
		},
		set(this: Admin, value: Permissions) {
			this.setDataValue('permissions', value.value);
		}
	})
	declare permissions: Permissions;

	@BeforeCreate
	@BeforeUpdate
	static hashPassword(instance: Admin) {
		if ((instance.changed() || []).indexOf('password') != -1) {
			instance.password = bcrypt.hashSync(instance.password, 10);
		}
	}

	validatePassword(password: string): boolean {
		return bcrypt.compareSync(password, this.password);
	}
}

export const sequelize = new Sequelize(building ? 'sqlite::memory:' : env.DATABASE_URI, {
	// only log sql queries in dev mode
	logging: dev ? console.log : false,
	models: [User, Report, Admin]
});
