ALTER TABLE `tblroles` ADD `description` VARCHAR(250) NULL AFTER `name`;
ALTER TABLE `tblroles` ADD `is_default` TINYINT(1) NOT NULL DEFAULT '0' AFTER `description`;

ALTER TABLE `tblstaff_invites` ADD `role_id` BIGINT(20) NULL DEFAULT NULL AFTER `staff_id`;

UPDATE `tblstaff` SET `role` = NULL;

CREATE TABLE `tblroles_static`(
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(150) NOT NULL,
    `description` VARCHAR(250) NULL,
    `is_default` TINYINT(1) NOT NULL DEFAULT 0,
    `permissions` LONGTEXT NOT NULL,
    PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tblroles_static`(`name`, `description`, `permissions`, `is_default`) VALUES ('Default','This is the default role.','{"customers":["view","create","edit","receive_notification"],"estimates":["view","create","edit","receive_notification"],"invoices":["view","create","edit","receive_notification"],"post_inspections":["view","create","edit","receive_notification"],"expenses":["view","create","edit","receive_notification"],"tasks":["view","create","edit","receive_notification"],"shopping_lists":["view","create","edit","receive_notification"],"suppliers_supplies":["view","create","edit","receive_notification"],"items":["view","create","edit","receive_notification"],"tools":["create","edit","checkin_checkout","receive_notification"],"payments":["view","create","edit","receive_notification","issue_refund"],"timelogs":["view","create","edit","clockin_clockout_for_other","add_manual_time_entry"],"reports":["view"],"term_contracts":["view","create","edit","receive_notification"],"job_schedule":["view","create","edit","receive_notification"],"team_chat_module":["create_group_chat","clear_chat"],"work_orders":["view","create","edit","receive_notification"],"mileage_logs":["view","create","edit","log_trips_for_others","receive_notification"],"calendar_event":["view","create","edit","receive_notification"],"staff":["view","create","edit"],"leads":["view"]}', '1');


INSERT INTO tblroles (`tenant_id`, name, description, permissions, is_default) SELECT t.id, r.name, r.description, r.permissions, r.is_default
FROM tbltenants t
JOIN tblroles_static r ON 1=1
ORDER BY t.id;
