SELECT 
        d.id,
        d.document_name,
        d.document_type,
        d.customer_id,
        COALESCE(c.company_name, CONCAT(c.first_name, " ", c.last_name)) as customer_name,
        d.version,
        d.is_signed,
        d.signed_date,
        d.expiry_date,
        d.created_at,
        CONCAT(u.first_name, " ", u.last_name) as uploader_name,
        d.description
    FROM crm_documents d
    LEFT JOIN customers c ON d.customer_id = c.id
    LEFT JOIN users u ON d.uploaded_by = u.id