-- ============================================================
-- seed-local-admin.sql
-- ERP Backend - Local development seed
-- ============================================================
--
-- Purpose:
--   Creates a local admin account, one active company and the
--   active membership between them.
--
-- Intended use:
--   Local/dev environments only.
--
-- Credentials:
--   email:    admin@local.test
--   password: Passw0rd123
--
-- Important:
--   Replace <BCRYPT_HASH_GENERADO> before running this script.
--
-- Generate bcrypt hash from the workspace:
--
--   node -e "const bcrypt=require('bcrypt'); bcrypt.hash('Admin123!', 10).then(console.log)"
--
-- This script is idempotent:
--   - It does not duplicate the admin account if the email exists.
--   - It does not duplicate the company if the slug exists.
--   - It does not duplicate the membership if it already exists.
--
-- If you need to force password reset for the existing admin,
-- uncomment the UPDATE block near the end.
-- ============================================================

SET @admin_id = UUID();

SET @admin_email = 'admin@local.test';

SET
    @admin_password_hash = '$2a$10$qgmmm34tJug4HydlKwcZxOVA5u5zoDTLE5lkH//sp55tl5au2wNQm';

SET
    @company_id1 = "5e4dc5b3-96b9-4968-9490-14bd032fec5f" -- UUID();
SET
    @company_slug1 = 'rodax';

SET @company_legal_name1 = 'Rodax Software S.L.';

SET @company_trade_name1 = 'Rodax';

SET @company_id2 = UUID();

SET @company_slug2 = 'company2';

SET @company_legal_name2 = 'Empresa 2 S.L.';

SET @company_trade_name2 = 'Empresa 2';

SET @company_id3 = UUID();

SET @company_slug3 = 'company3';

SET @company_legal_name3 = 'Empresa 2 S.L.';

SET @company_trade_name3 = 'Empresa 3';

-- ------------------------------------------------------------
-- Account admin
-- ------------------------------------------------------------

INSERT INTO
    identity_accounts (
        id,
        email,
        password_hash,
        name,
        avatar_url,
        language_code,
        status,
        created_at,
        updated_at
    )
SELECT
    @admin_id,
    @admin_email,
    @admin_password_hash,
    'Administrador',
    NULL,
    'es',
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
WHERE
    NOT EXISTS (
        SELECT 1
        FROM identity_accounts
        WHERE
            email = @admin_email
    );

-- Recover the real account id if the account already existed.
SELECT id INTO @admin_id
FROM identity_accounts
WHERE
    email = @admin_email
LIMIT 1;

-- ------------------------------------------------------------
-- Company
-- ------------------------------------------------------------

INSERT INTO
    companies (
        id,
        legal_name,
        trade_name,
        tin,
        slug,
        email,
        phone,
        website,
        status,
        created_at,
        updated_at
    )
SELECT
    @company_id,
    @company_legal_name,
    @company_trade_name,
    NULL,
    @company_slug,
    @admin_email,
    NULL,
    NULL,
    'active',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
WHERE
    NOT EXISTS (
        SELECT 1
        FROM companies
        WHERE
            slug = @company_slug
    );

-- Recover the real company id if the company already existed.
SELECT id INTO @company_id
FROM companies
WHERE
    slug = @company_slug
LIMIT 1;

-- ------------------------------------------------------------
-- Account-company membership
-- ------------------------------------------------------------

INSERT INTO
    identity_company_memberships (
        id,
        account_id,
        company_id,
        status,
        created_at,
        updated_at
    )
SELECT UUID(), @admin_id, @company_id, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE
    NOT EXISTS (
        SELECT 1
        FROM identity_company_memberships
        WHERE
            account_id = @admin_id
            AND company_id = @company_id
    );

-- ------------------------------------------------------------
-- Optional: force admin password/status update in local/dev
-- ------------------------------------------------------------
--
-- UPDATE identity_accounts
-- SET
--   password_hash = @admin_password_hash,
--   status = 'active',
--   updated_at = CURRENT_TIMESTAMP
-- WHERE email = @admin_email;

-- ------------------------------------------------------------
-- Final check
-- ------------------------------------------------------------

SELECT
    a.id AS account_id,
    a.email,
    a.status AS account_status,
    c.id AS company_id,
    c.legal_name,
    c.trade_name,
    c.slug,
    c.status AS company_status,
    m.status AS membership_status
FROM
    identity_accounts a
    JOIN identity_company_memberships m ON m.account_id = a.id
    JOIN companies c ON c.id = m.company_id
WHERE
    a.email = @admin_email
    AND c.slug = @company_slug;