/*
  MSIN - Acces evenement v2 (phase 1) - Seed
  Executer APRES MSIN-eve-acces-v2-phase1-tables.sql
*/

SET NAMES utf8mb4;


INSERT INTO `inscriptions_eve_permissions`
  (`perm_key`, `perm_group`, `perm_label_fr`, `perm_label_en`, `perm_description_fr`, `perm_description_en`, `perm_phase`, `perm_actif`, `perm_tri`)
VALUES
  ('events.list', 'api', 'Lister les evenements', 'List events',
   'GET /v1/events', NULL, 1, 1, 10),
  ('events.view', 'api', 'Voir un evenement', 'View event',
   'GET /v1/events/{id}', NULL, 1, 1, 20),
  ('registrations.view', 'api', 'Voir les inscriptions', 'View registrations',
   'GET /v1/events/{id}/registrations', NULL, 1, 1, 30),
  ('registrations.scan', 'api', 'Scanner QR', 'Scan QR',
   'Scan QR inscriptions mobile', NULL, 1, 1, 40),
  ('inscriptions.view', 'inscriptions', 'Inscriptions (lecture)', 'Registrations (read)',
   'Tableau inscriptions web promoteur', NULL, 2, 1, 110),
  ('inscriptions.edit', 'inscriptions', 'Inscriptions (modification)', 'Registrations (edit)',
   'Modifier ou annuler inscriptions', NULL, 2, 1, 120),
  ('inscriptions_gestion.view', 'inscriptions', 'Gestion inscription (web)', 'Registration management (web)',
   'Interface web de gestion des inscriptions', NULL, 2, 1, 130),
  ('dossards.view', 'dossards', 'Dossards (lecture)', 'Bibs (read)', NULL, NULL, 2, 1, 210),
  ('dossards.manage', 'dossards', 'Dossards (gestion)', 'Bibs (manage)', NULL, NULL, 2, 1, 220),
  ('epreuves.view', 'epreuves', 'Epreuves (lecture)', 'Races (read)', NULL, NULL, 2, 1, 310),
  ('epreuves.edit_qte', 'epreuves', 'Epreuves (quantites)', 'Races (quantities)', NULL, NULL, 2, 1, 320),
  ('rabais.view', 'rabais', 'Rabais (lecture)', 'Discounts (read)', NULL, NULL, 2, 1, 410),
  ('rabais.manage', 'rabais', 'Rabais (gestion)', 'Discounts (manage)', NULL, NULL, 2, 1, 420),
  ('emails.send', 'emails', 'Courriels', 'Emails', NULL, NULL, 2, 1, 510),
  ('reports.chrono', 'reports', 'Rapport chrono + questions', 'Chrono + questions report', NULL, NULL, 3, 1, 610),
  ('reports.finances', 'reports', 'Rapport finances', 'Finances report', NULL, NULL, 3, 1, 620),
  ('reports.finances_membership', 'reports', 'Finances adhesions', 'Membership finances', NULL, NULL, 3, 1, 625),
  ('reports.finances_events', 'reports', 'Finances evenements seul', 'Events-only finances', NULL, NULL, 3, 1, 626),
  ('reports.payments', 'reports', 'Rapport paiements', 'Payments report', NULL, NULL, 3, 1, 630),
  ('reports.adhesions', 'reports', 'Adhesions actives', 'Active memberships', NULL, NULL, 3, 1, 640),
  ('reports.adhesions_sales', 'reports', 'Ventes adhesions', 'Membership sales', NULL, NULL, 3, 1, 650),
  ('reports.finances_captains', 'reports', 'Finances capitaines', 'Captains finances', NULL, NULL, 3, 1, 660),
  ('reports.custom', 'reports', 'Rapports personnalises', 'Custom reports', NULL, NULL, 3, 1, 690),
  ('team.view', 'team', 'Voir l equipe', 'View team', NULL, NULL, 2, 1, 710),
  ('team.invite', 'team', 'Inviter des membres', 'Invite members', NULL, NULL, 2, 1, 720),
  ('team.manage', 'team', 'Gerer l equipe', 'Manage team', NULL, NULL, 2, 1, 730)
ON DUPLICATE KEY UPDATE
  `perm_group` = VALUES(`perm_group`),
  `perm_label_fr` = VALUES(`perm_label_fr`),
  `perm_label_en` = VALUES(`perm_label_en`),
  `perm_description_fr` = VALUES(`perm_description_fr`),
  `perm_description_en` = VALUES(`perm_description_en`),
  `perm_phase` = VALUES(`perm_phase`),
  `perm_actif` = VALUES(`perm_actif`),
  `perm_tri` = VALUES(`perm_tri`);


INSERT INTO `inscriptions_eve_roles`
  (`role_code`, `role_label_fr`, `role_label_en`, `role_description_fr`, `role_description_en`, `role_icone`, `role_systeme`, `role_actif`, `role_tri`)
VALUES
  ('owner', 'Proprietaire', 'Owner',
   'Acces complet sur l evenement',
   'Full access to the event',
   'fa-crown', 1, 1, 10),
  ('gestion_ops', 'Operations gestion', 'Management operations',
   'Gestion des inscriptions : evenements, inscriptions, scan QR',
   'Registration management: events, registrations, QR scan',
   'fa-mobile', 1, 1, 20),
  ('gestion_readonly', 'Gestion lecture seule', 'Management read-only',
   'Consulter evenements et inscriptions',
   'View events and registrations only',
   'fa-eye', 1, 1, 30),
  ('reports_finance', 'Finances', 'Finance',
   'Rapports finances et paiements uniquement',
   'Finance and payments reports only',
   'fa-money', 1, 1, 40),
  ('reports_ops', 'Operations + chrono', 'Operations + chrono',
   'Rapports chrono et operationnels sans finances',
   'Chrono and operational reports without finance',
   'fa-bar-chart', 1, 1, 50)
ON DUPLICATE KEY UPDATE
  `role_label_fr` = VALUES(`role_label_fr`),
  `role_label_en` = VALUES(`role_label_en`),
  `role_description_fr` = VALUES(`role_description_fr`),
  `role_description_en` = VALUES(`role_description_en`),
  `role_icone` = VALUES(`role_icone`),
  `role_systeme` = VALUES(`role_systeme`),
  `role_actif` = VALUES(`role_actif`),
  `role_tri` = VALUES(`role_tri`);


DELETE erp FROM inscriptions_eve_role_permissions erp
INNER JOIN inscriptions_eve_roles r ON r.role_id = erp.role_id
WHERE r.role_code = 'owner';

INSERT INTO inscriptions_eve_role_permissions (role_id, perm_key)
SELECT r.role_id, p.perm_key
FROM inscriptions_eve_roles r
CROSS JOIN inscriptions_eve_permissions p
WHERE r.role_code = 'owner'
  AND p.perm_actif = 1;


DELETE erp FROM inscriptions_eve_role_permissions erp
INNER JOIN inscriptions_eve_roles r ON r.role_id = erp.role_id
WHERE r.role_code = 'gestion_ops';

INSERT INTO inscriptions_eve_role_permissions (role_id, perm_key)
SELECT r.role_id, p.perm_key
FROM inscriptions_eve_roles r
INNER JOIN inscriptions_eve_permissions p
  ON p.perm_key IN ('events.list', 'events.view', 'registrations.view', 'registrations.scan')
 AND p.perm_actif = 1
WHERE r.role_code = 'gestion_ops';


DELETE erp FROM inscriptions_eve_role_permissions erp
INNER JOIN inscriptions_eve_roles r ON r.role_id = erp.role_id
WHERE r.role_code = 'gestion_readonly';

INSERT INTO inscriptions_eve_role_permissions (role_id, perm_key)
SELECT r.role_id, p.perm_key
FROM inscriptions_eve_roles r
INNER JOIN inscriptions_eve_permissions p
  ON p.perm_key IN ('events.list', 'events.view', 'registrations.view')
 AND p.perm_actif = 1
WHERE r.role_code = 'gestion_readonly';


DELETE erp FROM inscriptions_eve_role_permissions erp
INNER JOIN inscriptions_eve_roles r ON r.role_id = erp.role_id
WHERE r.role_code = 'reports_finance';

INSERT INTO inscriptions_eve_role_permissions (role_id, perm_key)
SELECT r.role_id, p.perm_key
FROM inscriptions_eve_roles r
INNER JOIN inscriptions_eve_permissions p
  ON p.perm_key IN (
    'events.list', 'events.view',
    'reports.finances', 'reports.finances_membership', 'reports.finances_events',
    'reports.payments', 'reports.finances_captains'
  )
 AND p.perm_actif = 1
WHERE r.role_code = 'reports_finance';


DELETE erp FROM inscriptions_eve_role_permissions erp
INNER JOIN inscriptions_eve_roles r ON r.role_id = erp.role_id
WHERE r.role_code = 'reports_ops';

INSERT INTO inscriptions_eve_role_permissions (role_id, perm_key)
SELECT r.role_id, p.perm_key
FROM inscriptions_eve_roles r
INNER JOIN inscriptions_eve_permissions p
  ON p.perm_key IN (
    'events.list', 'events.view',
    'registrations.view', 'inscriptions.view', 'inscriptions_gestion.view',
    'dossards.view', 'reports.chrono', 'reports.adhesions', 'reports.adhesions_sales', 'reports.custom'
  )
 AND p.perm_actif = 1
WHERE r.role_code = 'reports_ops';
