SET FOREIGN_KEY_CHECKS = 0;

-- Child tables
DELETE FROM role_has_permissions;
ALTER TABLE role_has_permissions AUTO_INCREMENT = 1;

DELETE FROM model_has_roles;
ALTER TABLE model_has_roles AUTO_INCREMENT = 1;

DELETE FROM model_has_permissions;
ALTER TABLE model_has_permissions AUTO_INCREMENT = 1;

-- Parent tables
DELETE FROM roles;
ALTER TABLE roles AUTO_INCREMENT = 1;

DELETE FROM permissions;
ALTER TABLE permissions AUTO_INCREMENT = 1;


START TRANSACTION;

/* ===============================
   INSERT PERMISSIONS
================================ */
INSERT INTO permissions (`parent`, `name`, `guard_name`, `created_at`, `updated_at`) VALUES
-- CNF
('cnf', 'cnf_create', 'web', NOW(), NOW()),
('cnf', 'cnf_edit', 'web', NOW(), NOW()),
('cnf', 'cnf_delete', 'web', NOW(), NOW()),
('cnf', 'cnf_view', 'web', NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

/* ===============================
   ASSIGN TO ROLE ID = 1
================================ */
INSERT IGNORE INTO role_has_permissions (role_id, permission_id)
SELECT 1, id
FROM permissions
WHERE name IN (
    'cnf_create',
    'cnf_edit',
    'cnf_delete',
    'cnf_view'
);

COMMIT;
