-- Create 'Admin' role if it doesn't exist
INSERT INTO "roles" ("id", "name", "description")
SELECT gen_random_uuid(), 'Admin', 'Administrator role with all permissions'
WHERE NOT EXISTS (SELECT 1 FROM "roles" WHERE "name" = 'Admin');

-- Create permissions if they don't exist
INSERT INTO "permissions" ("id", "name", "description")
SELECT gen_random_uuid(), 'manage:users', 'Permission to manage users'
WHERE NOT EXISTS (SELECT 1 FROM "permissions" WHERE "name" = 'manage:users');

INSERT INTO "permissions" ("id", "name", "description")
SELECT gen_random_uuid(), 'manage:projects', 'Permission to manage projects'
WHERE NOT EXISTS (SELECT 1 FROM "permissions" WHERE "name" = 'manage:projects');

INSERT INTO "permissions" ("id", "name", "description")
SELECT gen_random_uuid(), 'manage:roles', 'Permission to manage roles and permissions'
WHERE NOT EXISTS (SELECT 1 FROM "permissions" WHERE "name" = 'manage:roles');

INSERT INTO "permissions" ("id", "name", "description")
SELECT gen_random_uuid(), 'view:reports', 'Permission to view reports'
WHERE NOT EXISTS (SELECT 1 FROM "permissions" WHERE "name" = 'view:reports');

INSERT INTO "permissions" ("id", "name", "description")
SELECT gen_random_uuid(), 'manage:role-daily-hours', 'Permission to manage role daily hours'
WHERE NOT EXISTS (SELECT 1 FROM "permissions" WHERE "name" = 'manage:role-daily-hours');

-- Link 'Admin' role to all permissions
INSERT INTO "role_permissions" ("id", "roleId", "permissionId")
SELECT
    gen_random_uuid(),
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin'),
    (SELECT "id" FROM "permissions" WHERE "name" = 'manage:users')
WHERE NOT EXISTS (
    SELECT 1 FROM "role_permissions" rp
    JOIN "roles" r ON rp."roleId" = r."id"
    JOIN "permissions" p ON rp."permissionId" = p."id"
    WHERE r."name" = 'Admin' AND p."name" = 'manage:users'
);

INSERT INTO "role_permissions" ("id", "roleId", "permissionId")
SELECT
    gen_random_uuid(),
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin'),
    (SELECT "id" FROM "permissions" WHERE "name" = 'manage:projects')
WHERE NOT EXISTS (
    SELECT 1 FROM "role_permissions" rp
    JOIN "roles" r ON rp."roleId" = r."id"
    JOIN "permissions" p ON rp."permissionId" = p."id"
    WHERE r."name" = 'Admin' AND p."name" = 'manage:projects'
);

INSERT INTO "role_permissions" ("id", "roleId", "permissionId")
SELECT
    gen_random_uuid(),
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin'),
    (SELECT "id" FROM "permissions" WHERE "name" = 'manage:roles')
WHERE NOT EXISTS (
    SELECT 1 FROM "role_permissions" rp
    JOIN "roles" r ON rp."roleId" = r."id"
    JOIN "permissions" p ON rp."permissionId" = p."id"
    WHERE r."name" = 'Admin' AND p."name" = 'manage:roles'
);

INSERT INTO "role_permissions" ("id", "roleId", "permissionId")
SELECT
    gen_random_uuid(),
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin'),
    (SELECT "id" FROM "permissions" WHERE "name" = 'view:reports')
WHERE NOT EXISTS (
    SELECT 1 FROM "role_permissions" rp
    JOIN "roles" r ON rp."roleId" = r."id"
    JOIN "permissions" p ON rp."permissionId" = p."id"
    WHERE r."name" = 'Admin' AND p."name" = 'view:reports'
);

INSERT INTO "role_permissions" ("id", "roleId", "permissionId")
SELECT
    gen_random_uuid(),
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin'),
    (SELECT "id" FROM "permissions" WHERE "name" = 'manage:role-daily-hours')
WHERE NOT EXISTS (
    SELECT 1 FROM "role_permissions" rp
    JOIN "roles" r ON rp."roleId" = r."id"
    JOIN "permissions" p ON rp."permissionId" = p."id"
    WHERE r."name" = 'Admin' AND p."name" = 'manage:role-daily-hours'
);


-- Create default admin user if it doesn't exist
-- IMPORTANT: Replace 'hashed_password_here' with the actual hashed password for 'asdfgh'
INSERT INTO "users" ("id", "email", "name", "hashedPassword", "roleId")
SELECT
    gen_random_uuid(),
    'guri@infinitysoftsystems.com',
    'Default Admin',
    '$2b$10$Aa5yhGZr//nO.eSNq0hAL.RC.HSIJGXB29kskGVK2/5STTKIZY9Su',
    (SELECT "id" FROM "roles" WHERE "name" = 'Admin')
WHERE NOT EXISTS (SELECT 1 FROM "users" WHERE "email" = 'guri@infinitysoftsystems.com');