-- ═══════════════════════════════════════════════════════════════════════════
-- SEED DEMO: Agile Technology SRL (consulting firm) + 3 aziende clienti
-- Database : nis2_agile_db
-- Data     : 2026-05-29
-- Scope    : ambiente DEV/test. Idempotente (rieseguibile via ON DUPLICATE).
-- Rollback : vedi sezione finale (commentata) per cancellazione totale.
--
-- Tutti i dati sono INVENTATI ai fini test della pipeline
--   consulting_firm  →  consultant users  →  client organizations  →  KB visibility.
-- ═══════════════════════════════════════════════════════════════════════════
USE nis2_agile_db;
SET @now := NOW();

-- ── 1) CONSULTING FIRM ────────────────────────────────────────────────────
INSERT INTO consulting_firms
   (name, vat_number, fiscal_code, forma_giuridica, address, city, province, cap,
    phone, pec, website, plan, max_organizations, max_users, status, created_at)
VALUES
   ('Agile Technology SRL', 'IT12345670962', '12345670962', 'SRL',
    'Via dei Consulenti 42', 'Milano', 'MI', '20121',
    '+39 02 5500 0001', 'agiletech@pec.it', 'https://agile.software',
    'enterprise', 100, 20, 'active', @now)
ON DUPLICATE KEY UPDATE name = VALUES(name);

-- Nota: la tabella non ha UNIQUE su vat_number, quindi gestiamo "upsert manuale"
SET @firm_id := (SELECT id FROM consulting_firms
                 WHERE vat_number = 'IT12345670962' ORDER BY id LIMIT 1);

-- ── 2) CONSULENTI (users con role=consultant, legati al firm) ─────────────
-- Password test: "Consultant2026!" (hash bcrypt cost=10, riusabile per tutti)
SET @pwd := '$2y$10$Z6QwT5qg5sP9ZdYy3mFvgeKv6L9DkH3o0c2.M0Y0PnCqVTwQHvz/i';

INSERT INTO users (email, password_hash, full_name, phone, role, consulting_firm_id, is_active, created_at)
VALUES
 ('marco.ferri@agiletech.demo',  @pwd, 'Marco Ferri',  '+39 333 1112201', 'consultant', @firm_id, 1, @now),
 ('laura.greco@agiletech.demo',  @pwd, 'Laura Greco',  '+39 333 1112202', 'consultant', @firm_id, 1, @now),
 ('paolo.rossi@agiletech.demo',  @pwd, 'Paolo Rossi',  '+39 333 1112203', 'consultant', @firm_id, 1, @now)
ON DUPLICATE KEY UPDATE consulting_firm_id = VALUES(consulting_firm_id),
                        role = VALUES(role);

SET @u_marco := (SELECT id FROM users WHERE email='marco.ferri@agiletech.demo');
SET @u_laura := (SELECT id FROM users WHERE email='laura.greco@agiletech.demo');
SET @u_paolo := (SELECT id FROM users WHERE email='paolo.rossi@agiletech.demo');

-- ── 3) ORGANIZATIONS CLIENTI (3 aziende, settori diversi) ─────────────────
INSERT INTO organizations
  (name, vat_number, fiscal_code, sector, entity_type, voluntary_compliance,
   employee_count, annual_turnover_eur, country, city, address, website,
   contact_email, contact_phone, subscription_plan, consulting_firm_id, is_active, created_at)
VALUES
 ('Aurora Sanità S.p.A.',  'IT04501230965', '04501230965', 'health',
  'essential', 0, 480, 92000000.00, 'IT', 'Bergamo', 'Via degli Ospedali 8',
  'https://aurorasanita.demo', 'compliance@aurorasanita.demo', '+39 035 7001001',
  'professional', @firm_id, 1, @now),

 ('NordWater Utilities S.r.l.', 'IT05512340963', '05512340963', 'water',
  'essential', 0, 220, 41500000.00, 'IT', 'Brescia', 'Strada del Depuratore 14',
  'https://nordwater.demo', 'security@nordwater.demo', '+39 030 4002002',
  'professional', @firm_id, 1, @now),

 ('Logistica Veloce S.r.l.',  'IT06723450961', '06723450961', 'transport',
  'important', 0, 95, 18700000.00, 'IT', 'Verona', 'Viale dei Trasporti 27',
  'https://logisticaveloce.demo', 'it@logisticaveloce.demo', '+39 045 5003003',
  'free', @firm_id, 1, @now)
ON DUPLICATE KEY UPDATE consulting_firm_id = VALUES(consulting_firm_id);

SET @org_aurora    := (SELECT id FROM organizations WHERE vat_number='IT04501230965');
SET @org_nordwater := (SELECT id FROM organizations WHERE vat_number='IT05512340963');
SET @org_logistica := (SELECT id FROM organizations WHERE vat_number='IT06723450961');

-- ── 4) UTENTI INTERNI DELLE ORG CLIENTI (org_admin per ciascuna) ─────────
INSERT INTO users (email, password_hash, full_name, phone, role, is_active, created_at) VALUES
 ('admin@aurorasanita.demo',     @pwd, 'Giulia Bianchi (Aurora)',     '+39 035 7001010', 'org_admin', 1, @now),
 ('admin@nordwater.demo',        @pwd, 'Stefano Conti (NordWater)',   '+39 030 4002020', 'org_admin', 1, @now),
 ('admin@logisticaveloce.demo',  @pwd, 'Elena Marini (Logistica)',    '+39 045 5003030', 'org_admin', 1, @now)
ON DUPLICATE KEY UPDATE role = VALUES(role);

SET @u_admin_aur := (SELECT id FROM users WHERE email='admin@aurorasanita.demo');
SET @u_admin_nw  := (SELECT id FROM users WHERE email='admin@nordwater.demo');
SET @u_admin_log := (SELECT id FROM users WHERE email='admin@logisticaveloce.demo');

-- ── 5) user_organizations: org_admin "interno" delle 3 org + consulenti ──
INSERT INTO user_organizations (user_id, organization_id, role, is_primary, joined_at) VALUES
 (@u_admin_aur, @org_aurora,    'org_admin',  1, @now),
 (@u_admin_nw,  @org_nordwater, 'org_admin',  1, @now),
 (@u_admin_log, @org_logistica, 'org_admin',  1, @now),
 -- Marco è consulente delle 3 org clienti
 (@u_marco, @org_aurora,    'consultant', 0, @now),
 (@u_marco, @org_nordwater, 'consultant', 0, @now),
 (@u_marco, @org_logistica, 'consultant', 0, @now),
 -- Laura solo Aurora + NordWater
 (@u_laura, @org_aurora,    'consultant', 0, @now),
 (@u_laura, @org_nordwater, 'consultant', 0, @now),
 -- Paolo solo Logistica
 (@u_paolo, @org_logistica, 'consultant', 0, @now)
ON DUPLICATE KEY UPDATE role = VALUES(role);

-- ── 6) firm_org_assignments: mapping firm → org → consulente ─────────────
-- assigned_to = 0 → tutti i membri del firm vedono la org (default)
-- assigned_to = X → solo quel consulente è "lead"
INSERT INTO firm_org_assignments (consulting_firm_id, organization_id, assigned_to, assigned_by, created_at) VALUES
 (@firm_id, @org_aurora,    0,          @u_marco, @now),
 (@firm_id, @org_nordwater, 0,          @u_marco, @now),
 (@firm_id, @org_logistica, 0,          @u_paolo, @now),
 -- lead specifici (denota responsabilità primaria)
 (@firm_id, @org_aurora,    @u_marco,   @u_marco, @now),
 (@firm_id, @org_nordwater, @u_laura,   @u_marco, @now),
 (@firm_id, @org_logistica, @u_paolo,   @u_paolo, @now)
ON DUPLICATE KEY UPDATE created_at = created_at;

-- ── 7) Riepilogo finale ──────────────────────────────────────────────────
SELECT 'CONSULTING FIRM' AS section, id, name, plan, status FROM consulting_firms WHERE id=@firm_id
UNION ALL SELECT 'CONSULTANT', id, full_name, role, email FROM users WHERE consulting_firm_id=@firm_id
UNION ALL SELECT 'CLIENT ORG', id, name, sector, entity_type FROM organizations WHERE consulting_firm_id=@firm_id
UNION ALL SELECT 'FIRM-ORG MAP', id, CONCAT('firm=', consulting_firm_id), CONCAT('org=', organization_id), CONCAT('assigned_to=', assigned_to) FROM firm_org_assignments WHERE consulting_firm_id=@firm_id;

-- ═══════════════════════════════════════════════════════════════════════════
-- ROLLBACK (decommentare per cancellare TUTTI i dati demo Agile Technology)
-- ═══════════════════════════════════════════════════════════════════════════
-- SET @firm_id := (SELECT id FROM consulting_firms WHERE vat_number='IT12345670962');
-- DELETE FROM firm_org_assignments WHERE consulting_firm_id=@firm_id;
-- DELETE FROM user_organizations WHERE user_id IN (SELECT id FROM users WHERE consulting_firm_id=@firm_id);
-- DELETE FROM user_organizations WHERE organization_id IN (SELECT id FROM organizations WHERE consulting_firm_id=@firm_id);
-- DELETE FROM organizations WHERE consulting_firm_id=@firm_id;
-- DELETE FROM users WHERE consulting_firm_id=@firm_id;
-- DELETE FROM users WHERE email LIKE 'admin@%.demo' AND email IN
--   ('admin@aurorasanita.demo','admin@nordwater.demo','admin@logisticaveloce.demo');
-- DELETE FROM consulting_firms WHERE id=@firm_id;
