SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

create table `users` (
  `id` bigint unsigned not null auto_increment primary key,
  `name` varchar(255) not null,
  `email` varchar(255) not null,
  `email_verified_at` timestamp null,
  `password` varchar(255) not null,
  `remember_token` varchar(100) null,
  `last_login_at` datetime null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `users` add unique `users_email_unique`(`email`);

create table `password_resets` (
  `id` bigint unsigned not null auto_increment primary key,
  `email` varchar(255) not null,
  `token` varchar(255) not null,
  `created_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `password_resets` add index `password_resets_email_index`(`email`);

create table `roles` (
  `id` bigint unsigned not null auto_increment primary key,
  `name` varchar(255) not null,
  `display_name` varchar(255) null,
  `description` varchar(255) null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `roles` add unique `roles_name_unique`(`name`);

create table `permissions` (
  `id` bigint unsigned not null auto_increment primary key,
  `name` varchar(255) not null,
  `display_name` varchar(255) null,
  `description` varchar(255) null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `permissions` add unique `permissions_name_unique`(`name`);

create table `role_user` (
  `role_id` bigint unsigned not null,
  `user_id` bigint unsigned not null,
  `user_type` varchar(255) not null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `role_user` add constraint `role_user_role_id_foreign` foreign key (`role_id`) references `roles` (`id`) on delete cascade on update cascade;

alter table `role_user` add primary key `role_user_user_id_role_id_user_type_primary`(`user_id`, `role_id`, `user_type`);

create table `permission_user` (
  `permission_id` bigint unsigned not null,
  `user_id` bigint unsigned not null,
  `user_type` varchar(255) not null
 ) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

 alter table `permission_user` add constraint `permission_user_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade on update cascade;

alter table `permission_user` add primary key `permission_user_user_id_permission_id_user_type_primary`(`user_id`, `permission_id`, `user_type`);

create table `permission_role` (`permission_id` bigint unsigned not null, `role_id` bigint unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `permission_role` add constraint `permission_role_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade on update cascade;

alter table `permission_role` add constraint `permission_role_role_id_foreign` foreign key (`role_id`) references `roles` (`id`) on delete cascade on update cascade;

alter table `permission_role` add primary key `permission_role_permission_id_role_id_primary`(`permission_id`, `role_id`);
