-- Optional seed data for document version management module

INSERT INTO public.studies (
    id, code, name, status, created_at
) VALUES (
    'dddddddd-1111-2222-3333-444444444444', 'DOC-DEMO', 'Document Demo Study', 'DRAFT', now()
)
ON CONFLICT (code) DO NOTHING;

INSERT INTO public.workflow_templates (
    id, trial_id, name, description, is_active, created_by, created_at, updated_at
) VALUES
    (
        '11111111-aaaa-bbbb-cccc-111111111211',
        (SELECT id FROM public.studies WHERE code = 'DOC-DEMO'),
        '标准审批（PM 单签）',
        'PM 单节点审批，适用于一般文件',
        true,
        (SELECT id FROM public.users WHERE email = 'pm@example.com'),
        now(),
        now()
    ),
    (
        '11111111-aaaa-bbbb-cccc-111111111212',
        (SELECT id FROM public.studies WHERE code = 'DOC-DEMO'),
        '严格审批（CRA→PM）',
        '双节点审批，适用于关键文件',
        true,
        (SELECT id FROM public.users WHERE email = 'pm@example.com'),
        now(),
        now()
    )
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name,
    description = EXCLUDED.description,
    is_active = EXCLUDED.is_active,
    trial_id = EXCLUDED.trial_id,
    created_by = EXCLUDED.created_by,
    updated_at = EXCLUDED.updated_at;

INSERT INTO public.workflow_nodes (
    id, template_id, node_order, role, name, is_required, created_at
) VALUES
    (
        '22222222-aaaa-bbbb-cccc-222222222311',
        '11111111-aaaa-bbbb-cccc-111111111211',
        1,
        'PM',
        '项目经理审批',
        true,
        now()
    ),
    (
        '22222222-aaaa-bbbb-cccc-222222222312',
        '11111111-aaaa-bbbb-cccc-111111111212',
        1,
        'CRA',
        'CRA 初审',
        true,
        now()
    ),
    (
        '22222222-aaaa-bbbb-cccc-222222222313',
        '11111111-aaaa-bbbb-cccc-111111111212',
        2,
        'PM',
        '项目经理复审',
        true,
        now()
    )
ON CONFLICT (id) DO UPDATE
SET template_id = EXCLUDED.template_id,
    node_order = EXCLUDED.node_order,
    role = EXCLUDED.role,
    name = EXCLUDED.name,
    is_required = EXCLUDED.is_required;

INSERT INTO public.documents (
    id, trial_id, site_id, doc_no, doc_type, title, scope_type, owner_id, status, created_at, updated_at
) VALUES (
    'eeeeeeee-1111-2222-3333-444444444444',
    (SELECT id FROM public.studies WHERE code = 'DOC-DEMO'),
    NULL,
    'SOP-001',
    'SOP',
    'Sample SOP for Versioning',
    'GLOBAL',
    (SELECT id FROM public.users WHERE email = 'pm@example.com'),
    'ACTIVE',
    now(),
    now()
)
ON CONFLICT (trial_id, doc_no) DO NOTHING;

INSERT INTO public.documents (
    id, trial_id, site_id, doc_no, doc_type, title, scope_type, owner_id, status, created_at, updated_at
) VALUES (
    'eeeeeeee-1111-2222-3333-444444444445',
    (SELECT id FROM public.studies WHERE code = 'DOC-DEMO'),
    (SELECT id FROM public.sites WHERE study_id = (SELECT id FROM public.studies WHERE code = 'DOC-DEMO') LIMIT 1),
    'SITE-SOP-001',
    'SOP',
    'DOC-DEMO 分中心 SOP',
    'SITE',
    (SELECT id FROM public.users WHERE email = 'pm@example.com'),
    'ACTIVE',
    now(),
    now()
)
ON CONFLICT (trial_id, doc_no) DO NOTHING;

INSERT INTO public.document_versions (
    id, document_id, version_no, status, file_uri, file_hash, file_size, mime_type, change_summary, created_by, created_at
) VALUES (
    'ffffffff-1111-2222-3333-444444444444',
    (SELECT id FROM public.documents WHERE doc_no = 'SOP-001' AND trial_id = (SELECT id FROM public.studies WHERE code = 'DOC-DEMO')),
    'V1.0',
    'DRAFT',
    '/uploads/docs/SOP-001/V1.0/sample.pdf',
    '9f2b4c6d8e0f1234567890abcdef1234567890abcdef1234567890abcdef1234',
    2048,
    'application/pdf',
    '初稿',
    (SELECT id FROM public.users WHERE email = 'pm@example.com'),
    now()
)
ON CONFLICT (document_id, version_no) DO NOTHING;

INSERT INTO public.document_versions (
    id, document_id, version_no, status, file_uri, file_hash, file_size, mime_type, change_summary, created_by, created_at
) VALUES (
    'ffffffff-1111-2222-3333-444444444445',
    (SELECT id FROM public.documents WHERE doc_no = 'SITE-SOP-001' AND trial_id = (SELECT id FROM public.studies WHERE code = 'DOC-DEMO')),
    '1.0',
    'EFFECTIVE',
    '/code/app/uploads/documents/demo/SITE-SOP-001-v1.pdf',
    'a3b2c1d0e9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c3b2a1f0e9d8c7b6a5f4e3d2',
    2048,
    'application/pdf',
    '分中心 SOP 首版',
    (SELECT id FROM public.users WHERE email = 'pm@example.com'),
    now()
)
ON CONFLICT (document_id, version_no) DO NOTHING;
