-- name: ListDocuments :many
SELECT 
    d.id,
    d.type,
    d.created,
    d.user_id,
    d.doc_page_count,
    d.pdf_filename,
    d.pdf_created_at,
    d.pdf_created_by_user_id,
    d.cmc_reference,
    d.revision,
    d.email_sent_at,
    d.email_sent_by_user_id,
    u.first_name as user_first_name,
    u.last_name as user_last_name,
    u.username as user_username,
    pdf_creator.first_name as pdf_creator_first_name,
    pdf_creator.last_name as pdf_creator_last_name,
    pdf_creator.username as pdf_creator_username
FROM documents d
LEFT JOIN users u ON d.user_id = u.id
LEFT JOIN users pdf_creator ON d.pdf_created_by_user_id = pdf_creator.id
ORDER BY d.id DESC
LIMIT 1000;

-- name: ListDocumentsByType :many
SELECT 
    d.id,
    d.type,
    d.created,
    d.user_id,
    d.doc_page_count,
    d.pdf_filename,
    d.pdf_created_at,
    d.pdf_created_by_user_id,
    d.cmc_reference,
    d.revision,
    d.email_sent_at,
    d.email_sent_by_user_id,
    u.first_name as user_first_name,
    u.last_name as user_last_name,
    u.username as user_username,
    pdf_creator.first_name as pdf_creator_first_name,
    pdf_creator.last_name as pdf_creator_last_name,
    pdf_creator.username as pdf_creator_username,
    COALESCE(ec.name, ic.name, '') as customer_name,
    e.title as enquiry_title
FROM documents d
LEFT JOIN users u ON d.user_id = u.id
LEFT JOIN users pdf_creator ON d.pdf_created_by_user_id = pdf_creator.id
LEFT JOIN enquiries e ON d.type IN ('quote', 'orderAck') AND d.cmc_reference = e.title
LEFT JOIN customers ec ON e.customer_id = ec.id
LEFT JOIN invoices i ON d.type = 'invoice' AND d.cmc_reference = i.title
LEFT JOIN customers ic ON i.customer_id = ic.id
WHERE d.type = ?
ORDER BY d.id DESC
LIMIT 1000;

-- name: GetDocumentByID :one
SELECT 
    d.id,
    d.type,
    d.created,
    d.user_id,
    d.doc_page_count,
    d.pdf_filename,
    d.pdf_created_at,
    d.pdf_created_by_user_id,
    d.cmc_reference,
    d.revision,
    d.shipping_details,
    d.bill_to,
    d.ship_to,
    d.email_sent_at,
    d.email_sent_by_user_id,
    u.first_name as user_first_name,
    u.last_name as user_last_name,
    u.username as user_username,
    pdf_creator.first_name as pdf_creator_first_name,
    pdf_creator.last_name as pdf_creator_last_name,
    pdf_creator.username as pdf_creator_username,
    COALESCE(ec.name, ic.name, '') as customer_name,
    e.title as enquiry_title
FROM documents d
LEFT JOIN users u ON d.user_id = u.id
LEFT JOIN users pdf_creator ON d.pdf_created_by_user_id = pdf_creator.id
LEFT JOIN enquiries e ON d.type IN ('quote', 'orderAck') AND d.cmc_reference = e.title
LEFT JOIN customers ec ON e.customer_id = ec.id
LEFT JOIN invoices i ON d.type = 'invoice' AND d.cmc_reference = i.title
LEFT JOIN customers ic ON i.customer_id = ic.id
WHERE d.id = ?;

-- name: CreateDocument :execresult
INSERT INTO documents (
    type,
    created,
    user_id,
    doc_page_count,
    cmc_reference,
    pdf_filename,
    pdf_created_at,
    pdf_created_by_user_id,
    shipping_details,
    revision,
    bill_to,
    ship_to,
    email_sent_at,
    email_sent_by_user_id
) VALUES (
    ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);

-- name: UpdateDocument :exec
UPDATE documents 
SET 
    type = ?,
    user_id = ?,
    doc_page_count = ?,
    cmc_reference = ?,
    pdf_filename = ?,
    pdf_created_at = ?,
    pdf_created_by_user_id = ?,
    shipping_details = ?,
    revision = ?,
    bill_to = ?,
    ship_to = ?,
    email_sent_at = ?,
    email_sent_by_user_id = ?
WHERE id = ?;

-- name: ArchiveDocument :exec
DELETE FROM documents 
WHERE id = ?;

-- name: UnarchiveDocument :exec
-- Note: Unarchiving not supported as documents table doesn't have an archived column
-- This is a no-op for compatibility
SELECT 1 WHERE ? = ?;

-- name: SearchDocuments :many
SELECT 
    d.id,
    d.type,
    d.created,
    d.user_id,
    d.doc_page_count,
    d.pdf_filename,
    d.pdf_created_at,
    d.pdf_created_by_user_id,
    d.cmc_reference,
    d.revision,
    d.email_sent_at,
    d.email_sent_by_user_id,
    u.first_name as user_first_name,
    u.last_name as user_last_name,
    u.username as user_username,
    pdf_creator.first_name as pdf_creator_first_name,
    pdf_creator.last_name as pdf_creator_last_name,
    pdf_creator.username as pdf_creator_username,
    COALESCE(ec.name, ic.name, '') as customer_name,
    e.title as enquiry_title
FROM documents d
LEFT JOIN users u ON d.user_id = u.id
LEFT JOIN users pdf_creator ON d.pdf_created_by_user_id = pdf_creator.id
LEFT JOIN enquiries e ON d.type IN ('quote', 'orderAck') AND d.cmc_reference = e.title
LEFT JOIN customers ec ON e.customer_id = ec.id
LEFT JOIN invoices i ON d.type = 'invoice' AND d.cmc_reference = i.title
LEFT JOIN customers ic ON i.customer_id = ic.id
WHERE (
    d.pdf_filename LIKE ? OR
    d.cmc_reference LIKE ? OR
    COALESCE(ec.name, ic.name) LIKE ? OR
    e.title LIKE ? OR
    u.username LIKE ?
)
ORDER BY d.id DESC
LIMIT 1000;

-- name: UpdateDocumentPDFInfo :exec
UPDATE documents 
SET 
    pdf_filename = ?,
    pdf_created_at = ?,
    pdf_created_by_user_id = ?
WHERE id = ?;

-- name: GetPurchaseOrderByDocumentID :one
SELECT 
    po.id,
    po.title,
    po.principle_id,
    po.principle_reference,
    po.issue_date,
    po.ordered_from,
    po.dispatch_by,
    po.deliver_to,
    po.shipping_instructions
FROM purchase_orders po
JOIN documents d ON d.cmc_reference = po.title
WHERE d.id = ? AND d.type = 'purchaseOrder';