-- ============================================================
-- SEED: Tenant demo + Tenant acme + utenti per sviluppo locale
--
-- Tenant demo:
--   Admin:    admin@demo.pechub.it   / Demo@PEChub2026!
--   Operator: operator@demo.pechub.it / Oper@PEChub2026!
--
-- Tenant acme (secondo tenant per test isolamento):
--   Admin:    admin@acme.pechub.it  / Acme@PEChub2026!
--
-- Super Admin (cross-tenant):
--   superadmin@pechub.it / SuperAdmin@PEChub2026!
--
-- Esegui con: make seed
-- ============================================================

-- Disabilita RLS temporaneamente per il seed
SET session_replication_role = replica;

-- ── Tenant demo ──────────────────────────────────────────────────────────────
INSERT INTO tenants (id, slug, name, plan, is_active, max_mailboxes, max_users)
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'demo',
    'Demo Azienda SRL',
    'pro',
    TRUE,
    10,
    20
)
ON CONFLICT (slug) DO NOTHING;

-- ── Tenant acme (secondo tenant per test isolamento) ─────────────────────────
INSERT INTO tenants (id, slug, name, plan, is_active, max_mailboxes, max_users)
VALUES (
    '22222222-2222-2222-2222-222222222222',
    'acme',
    'Acme Corp SpA',
    'starter',
    TRUE,
    5,
    10
)
ON CONFLICT (slug) DO NOTHING;

-- ── Utente super_admin (ruolo globale, associato al tenant demo) ──────────────
-- Password: SuperAdmin@PEChub2026! (bcrypt hash)
INSERT INTO users (id, tenant_id, email, password_hash, full_name, role, is_active)
VALUES (
    '00000000-0000-0000-0000-000000000001',
    '11111111-1111-1111-1111-111111111111',
    'superadmin@pechub.it',
    '$2b$12$XVHODc6nahpMm.XO5ifDku77IFDqCcMkJpSc7.uwElpML4wo3gfQu',
    'Super Admin PEChub',
    'super_admin',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- ── Admin del tenant demo ─────────────────────────────────────────────────────
-- Password: Demo@PEChub2026! (bcrypt hash)
INSERT INTO users (id, tenant_id, email, password_hash, full_name, role, is_active)
VALUES (
    '11111111-0000-0000-0000-000000000001',
    '11111111-1111-1111-1111-111111111111',
    'admin@demo.pechub.it',
    '$2b$12$xBbzU5vPAibZWx/jnEwJoO8aAAK9EdIBMzQbo7naD22t37EJeIy9q',
    'Admin Demo',
    'admin',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- ── Operator del tenant demo ──────────────────────────────────────────────────
-- Password: Oper@PEChub2026! (bcrypt hash)
INSERT INTO users (id, tenant_id, email, password_hash, full_name, role, is_active)
VALUES (
    '11111111-0000-0000-0000-000000000002',
    '11111111-1111-1111-1111-111111111111',
    'operator@demo.pechub.it',
    '$2b$12$8stUJfKKTB5Tqjrd3Aamm.sOrQe9T0kygbkTbN7raItjhD0exdyVm',
    'Operatore Demo',
    'operator',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- ── Admin del tenant acme ─────────────────────────────────────────────────────
-- Password: Acme@PEChub2026! (bcrypt hash)
INSERT INTO users (id, tenant_id, email, password_hash, full_name, role, is_active)
VALUES (
    '22222222-0000-0000-0000-000000000001',
    '22222222-2222-2222-2222-222222222222',
    'admin@acme.pechub.it',
    '$2b$12$lGN3ckunwsI2pS2VKPjLAemnvJgv3DzPddcv4W4KzwjeVvRQh.jhO',
    'Admin Acme Corp',
    'admin',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- Ripristina RLS
SET session_replication_role = DEFAULT;

-- Verifica
DO $$
BEGIN
    RAISE NOTICE '✅ Seed completato!';
    RAISE NOTICE '   Tenant demo:  11111111-1111-1111-1111-111111111111';
    RAISE NOTICE '   Tenant acme:  22222222-2222-2222-2222-222222222222';
    RAISE NOTICE '   SuperAdmin:   superadmin@pechub.it / SuperAdmin@PEChub2026!';
    RAISE NOTICE '   Admin demo:   admin@demo.pechub.it / Demo@PEChub2026!';
    RAISE NOTICE '   Operator demo: operator@demo.pechub.it / Oper@PEChub2026!';
    RAISE NOTICE '   Admin acme:   admin@acme.pechub.it / Acme@PEChub2026!';
END
$$;
