-- ============================================================
-- SEED: Tenant demo + utenti per sviluppo locale
-- 
-- Credenziali:
--   Admin:    admin@demo.pechub.it  / Demo@PEChub2026!
--   Operator: operator@demo.pechub.it / Oper@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;

-- Utente super_admin (global, senza tenant specifico usa il 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$y2yq6X2f3dZi22wqWZd1aumP03IU6OWrrevRMFj9054aGnUms116W', -- SuperAdmin@PEChub2026!
    'Super Admin PEChub',
    'super_admin',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- Utente 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$PmyaJvF0i7ACFR39k6hfMO2.6U.FVPYma.7OyXyrGuGuokiJOfX8y', -- Demo@PEChub2026!
    'Admin Demo',
    'admin',
    TRUE
)
ON CONFLICT (tenant_id, email) DO NOTHING;

-- Utente 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$Z0REc7flPCD3Sb8fZHsuW.Uk2X4JiJO7HhTajNSuPiQgzppkCDmLu', -- Oper@PEChub2026!
    'Operatore Demo',
    'operator',
    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 '   Admin: admin@demo.pechub.it / Demo@PEChub2026!';
    RAISE NOTICE '   Operator: operator@demo.pechub.it / Oper@PEChub2026!';
END
$$;
