-- Debug script untuk mengecek masalah approval flow

-- 1. Cek user level 5
SELECT 
    id,
    level_name,
    level_number,
    is_approval_active,
    client_id
FROM user_levels 
WHERE id = 5;

-- 2. Cek user dengan level 5
SELECT 
    u.id,
    u.name,
    u.user_level_id,
    ul.level_name,
    ul.level_number,
    ul.is_approval_active
FROM users u
JOIN user_levels ul ON u.user_level_id = ul.id
WHERE u.user_level_id = 5;

-- 3. Cek default workflow
SELECT 
    id,
    name,
    is_active,
    is_default,
    client_id
FROM approval_workflows 
WHERE is_default = true 
AND is_active = true;

-- 4. Cek workflow steps
SELECT 
    aws.id,
    aws.workflow_id,
    aws.step_order,
    aws.step_name,
    aws.required_user_level_id,
    aws.condition_type,
    aws.condition_value,
    aws.branch_name,
    aw.name as workflow_name
FROM approval_workflow_steps aws
JOIN approval_workflows aw ON aws.workflow_id = aw.id
WHERE aw.is_default = true
ORDER BY aws.step_order, aws.branch_order;

-- 5. Cek artikel yang baru dibuat
SELECT 
    id,
    title,
    created_by_id,
    workflow_id,
    current_approval_step,
    status_id,
    bypass_approval,
    approval_exempt,
    created_at
FROM articles 
WHERE title = 'Test Tni Artikel 1'
ORDER BY created_at DESC;

-- 6. Cek approval flows
SELECT 
    aaf.id,
    aaf.article_id,
    aaf.workflow_id,
    aaf.current_step,
    aaf.current_branch,
    aaf.status_id,
    aaf.submitted_by_id,
    aaf.submitted_at,
    a.title as article_title
FROM article_approval_flows aaf
JOIN articles a ON aaf.article_id = a.id
WHERE a.title = 'Test Tni Artikel 1'
ORDER BY aaf.created_at DESC;

-- 7. Cek legacy approval records
SELECT 
    aa.id,
    aa.article_id,
    aa.approval_by,
    aa.status_id,
    aa.message,
    aa.approval_at_level,
    a.title as article_title
FROM article_approvals aa
JOIN articles a ON aa.article_id = a.id
WHERE a.title = 'Test Tni Artikel 1'
ORDER BY aa.created_at DESC;
