INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'ALPHA-NET-MGR-001', 'manager.alpha@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Sophie', 'Réseau', '0600000003', 'active', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'manager.alpha@example.test');

INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'AGENCY-MGR-001', 'chef.agence@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Julien', 'Bernard', '0600000004', 'active', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'chef.agence@example.test');

INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'AGENCY-AGENT-001', 'agent.agence@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Nina', 'Petit', '0600000005', 'active',
       (SELECT u.id FROM users u WHERE u.email = 'chef.agence@example.test' LIMIT 1)
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'agent.agence@example.test');

INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'NET-IND-001', 'mandataire@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Marc', 'Leroy', '0600000006', 'active', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'mandataire@example.test');

INSERT INTO user_platform_roles (user_id, role_code, is_active)
SELECT u.id, 'super_admin', 1
FROM users u
WHERE u.email = 'admin@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_platform_roles upr WHERE upr.user_id = u.id AND upr.role_code = 'super_admin');

INSERT INTO user_network_memberships (user_id, network_id, role_code, is_active)
SELECT u.id, 1, 'network_manager', 1
FROM users u
WHERE u.email = 'manager.alpha@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_network_memberships unm WHERE unm.user_id = u.id AND unm.network_id = 1 AND unm.role_code = 'network_manager');

INSERT INTO user_shop_memberships (user_id, shop_id, role_code, is_active)
SELECT u.id, 2, 'agency_manager', 1
FROM users u
WHERE u.email = 'chef.agence@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_shop_memberships usm WHERE usm.user_id = u.id AND usm.shop_id = 2 AND usm.role_code = 'agency_manager');

INSERT INTO user_shop_memberships (user_id, shop_id, role_code, is_active)
SELECT u.id, 2, 'agency_agent', 1
FROM users u
WHERE u.email = 'agent.agence@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_shop_memberships usm WHERE usm.user_id = u.id AND usm.shop_id = 2 AND usm.role_code = 'agency_agent');

INSERT INTO user_network_memberships (user_id, network_id, role_code, is_active)
SELECT u.id, 1, 'independent_agent', 1
FROM users u
WHERE u.email = 'mandataire@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_network_memberships unm WHERE unm.user_id = u.id AND unm.network_id = 1 AND unm.role_code = 'independent_agent');

INSERT INTO credit_accounts (owner_type, owner_id, currency, balance_cached)
SELECT 'user', u.id, 'EUR', 50.00
FROM users u
WHERE u.email = 'agent.agence@example.test'
  AND NOT EXISTS (
      SELECT 1 FROM credit_accounts ca WHERE ca.owner_type = 'user' AND ca.owner_id = u.id
  );

INSERT INTO credit_transactions (account_id, movement_type, amount, reference, reason, created_by_user_id)
SELECT ca.id, 'grant', 50.00, 'AGENT-WELCOME', 'Crédit de bienvenue agent',
       (SELECT u.id FROM users u WHERE u.email = 'chef.agence@example.test' LIMIT 1)
FROM credit_accounts ca
INNER JOIN users agent ON agent.id = ca.owner_id
WHERE ca.owner_type = 'user'
  AND agent.email = 'agent.agence@example.test'
  AND NOT EXISTS (
      SELECT 1 FROM credit_transactions ct WHERE ct.account_id = ca.id AND ct.reference = 'AGENT-WELCOME'
  );


INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'GRAPHICS-OP-001', 'graphiste.alpha@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Lina', 'Graphisme', '0600000007', 'active', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'graphiste.alpha@example.test');

INSERT INTO users (network_id, external_identifier, email, password_hash, first_name, last_name, phone, status, created_by_user_id)
SELECT 1, 'PROD-OP-001', 'production.alpha@example.test', '$2y$12$e40bYvCEAPIF10NVwHM8C..LtYxxhyowVcZSUpwNi3SFuUsfDi.le', 'Hugo', 'Production', '0600000008', 'active', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'production.alpha@example.test');

INSERT INTO user_network_memberships (user_id, network_id, role_code, is_active)
SELECT u.id, 1, 'graphics_operator', 1
FROM users u
WHERE u.email = 'graphiste.alpha@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_network_memberships unm WHERE unm.user_id = u.id AND unm.network_id = 1 AND unm.role_code = 'graphics_operator');

INSERT INTO user_network_memberships (user_id, network_id, role_code, is_active)
SELECT u.id, 1, 'production_operator', 1
FROM users u
WHERE u.email = 'production.alpha@example.test'
  AND NOT EXISTS (SELECT 1 FROM user_network_memberships unm WHERE unm.user_id = u.id AND unm.network_id = 1 AND unm.role_code = 'production_operator');

