-- ============================================================
-- 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: Admin123!
--
-- Generate bcrypt hash from the workspace:
--
--   node -e "const bcrypt=require('bcrypt'); bcrypt.hash('Admin123!', 10).then(console.log)"
--
-- Replace the hash below if you want to regenerate it.
-- 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.
-- ============================================================

SET @admin_id = UUID();
SET @company_id = UUID();

SET @admin_email = 'admin@local.test';
SET @admin_password_hash = '$2b$10$6m6Nh2OpDy9MlQF18KOueOzLSCHybcg8yu1JyG0XxjgxB5Qx2dMdG';

SET @company_slug = 'rodax';
SET @company_legal_name = 'Rodax Software S.L.';
SET @company_trade_name = 'Rodax';

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
    );

SELECT id INTO @admin_id
FROM identity_accounts
WHERE
    email = @admin_email
LIMIT 1;

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
    );

SELECT id INTO @company_id
FROM companies
WHERE
    slug = @company_slug
LIMIT 1;

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
    );

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;
