CREATE TABLE `audit_log` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`action` text NOT NULL,
	`content` text DEFAULT 'NULL',
	`actor_ip` text DEFAULT 'NULL',
	`actor_ua` text DEFAULT 'NULL',
	`flagged` tinyint NOT NULL DEFAULT 0,
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`actorId` int(11) DEFAULT 'NULL'
);--> statement-breakpoint

CREATE TABLE `document` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`title` text NOT NULL,
	`slug` text NOT NULL,
	`body` text NOT NULL,
	`authorId` int(11) DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)'
);--> statement-breakpoint

CREATE TABLE `o_auth2_client` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`client_id` varchar(36) NOT NULL,
	`client_secret` text NOT NULL,
	`title` varchar(255) NOT NULL,
	`description` text DEFAULT 'NULL',
	`scope` text DEFAULT 'NULL',
	`grants` text NOT NULL DEFAULT ''authorization_code'',
	`activated` tinyint NOT NULL DEFAULT 0,
	`verified` tinyint NOT NULL DEFAULT 0,
	`pictureId` int(11) DEFAULT 'NULL',
	`ownerId` int(11) DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	CONSTRAINT `IDX_e9d16c213910ad57bd05e97b42` UNIQUE(`client_id`)
);--> statement-breakpoint

CREATE TABLE `o_auth2_client_authorization` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`scope` text DEFAULT 'NULL',
	`expires_at` timestamp NOT NULL DEFAULT 'current_timestamp()',
	`clientId` int(11) DEFAULT 'NULL',
	`userId` int(11) DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)'
);--> statement-breakpoint

CREATE TABLE `o_auth2_client_url` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`url` varchar(255) NOT NULL,
	`type` enum('redirect_uri','terms','privacy','website') NOT NULL,
	`created_at` timestamp(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` timestamp(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`clientId` int(11) DEFAULT 'NULL'
);--> statement-breakpoint

CREATE TABLE `o_auth2_token` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`type` enum('code','access_token','refresh_token') NOT NULL,
	`token` text NOT NULL,
	`scope` text DEFAULT 'NULL',
	`expires_at` timestamp NOT NULL DEFAULT 'current_timestamp()',
	`userId` int(11) DEFAULT 'NULL',
	`clientId` int(11) DEFAULT 'NULL',
	`nonce` text DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`pcke` text DEFAULT 'NULL'
);--> statement-breakpoint

CREATE TABLE `privilege` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`name` text NOT NULL
);--> statement-breakpoint

CREATE TABLE `upload` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`original_name` varchar(255) NOT NULL,
	`mimetype` varchar(255) NOT NULL,
	`file` varchar(255) NOT NULL,
	`uploaderId` int(11) DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)'
);--> statement-breakpoint

CREATE TABLE `user` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`uuid` varchar(36) NOT NULL,
	`username` varchar(26) NOT NULL,
	`email` varchar(255) NOT NULL,
	`display_name` varchar(32) NOT NULL,
	`password` text DEFAULT 'NULL',
	`activated` tinyint NOT NULL DEFAULT 0,
	`activity_at` timestamp NOT NULL DEFAULT 'current_timestamp()',
	`pictureId` int(11) DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	`updated_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)',
	CONSTRAINT `IDX_a95e949168be7b7ece1a2382fe` UNIQUE(`uuid`),
	CONSTRAINT `IDX_78a916df40e02a9deb1c4b75ed` UNIQUE(`username`),
	CONSTRAINT `IDX_e12875dfb3b1d92d7d7c5377e2` UNIQUE(`email`)
);--> statement-breakpoint

CREATE TABLE `user_privileges_privilege` (
	`userId` int(11) NOT NULL,
	`privilegeId` int(11) NOT NULL
);--> statement-breakpoint

CREATE TABLE `user_token` (
	`id` int(11) AUTO_INCREMENT NOT NULL,
	`token` text NOT NULL,
	`type` enum('generic','activation','deactivation','password','login','gdpr','totp','public_key','recovery') NOT NULL,
	`expires_at` timestamp DEFAULT 'NULL',
	`userId` int(11) DEFAULT 'NULL',
	`nonce` text DEFAULT 'NULL',
	`created_at` datetime(6) NOT NULL DEFAULT 'current_timestamp(6)'
);--> statement-breakpoint

ALTER TABLE `audit_log` ADD CONSTRAINT `FK_cb6aa6f6fd56f08eafb60316225` FOREIGN KEY (`actorId`) REFERENCES `user`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `document` ADD CONSTRAINT `FK_6a2eb13cadfc503989cbe367572` FOREIGN KEY (`authorId`) REFERENCES `user`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_client` ADD CONSTRAINT `FK_4a6c878506b872e85b3d07f6252` FOREIGN KEY (`ownerId`) REFERENCES `user`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_client` ADD CONSTRAINT `FK_e8d65b1eec13474e493420517d7` FOREIGN KEY (`pictureId`) REFERENCES `upload`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_client_authorization` ADD CONSTRAINT `FK_8227110f58510b7233f3db90cfb` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_client_authorization` ADD CONSTRAINT `FK_9ca9ebb654e7ce71954d5fdb281` FOREIGN KEY (`clientId`) REFERENCES `o_auth2_client`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_client_url` ADD CONSTRAINT `FK_aca59c7bdd65987487eea98d00f` FOREIGN KEY (`clientId`) REFERENCES `o_auth2_client`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_token` ADD CONSTRAINT `FK_3ecb760b321ef9bbab635f05b45` FOREIGN KEY (`clientId`) REFERENCES `o_auth2_client`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `o_auth2_token` ADD CONSTRAINT `FK_81ffb9b8d672cf3af1af9e789f3` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `upload` ADD CONSTRAINT `FK_7b8d52838a953b188255682597b` FOREIGN KEY (`uploaderId`) REFERENCES `user`(`id`) ON DELETE set null ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE `user` ADD CONSTRAINT `FK_7478a15985dbfa32ed5fc77a7a1` FOREIGN KEY (`pictureId`) REFERENCES `upload`(`id`) ON DELETE set null ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE `user_privileges_privilege` ADD CONSTRAINT `FK_0664a7ff494a1859a09014c0f17` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE `user_privileges_privilege` ADD CONSTRAINT `FK_e71171f4ed20bc8564a1819d0b7` FOREIGN KEY (`privilegeId`) REFERENCES `privilege`(`id`) ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE `user_token` ADD CONSTRAINT `FK_d37db50eecdf9b8ce4eedd2f918` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `IDX_0664a7ff494a1859a09014c0f1` ON `user_privileges_privilege` (`userId`);--> statement-breakpoint
CREATE INDEX `IDX_e71171f4ed20bc8564a1819d0b` ON `user_privileges_privilege` (`privilegeId`);
