-- name: GetEnquiry :one
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       contact.email as contact_email,
       contact.mobile as contact_mobile,
       contact.direct_phone as contact_direct_phone,
       contact.phone as contact_phone,
       contact.phone_extension as contact_phone_extension,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name,
       st.name as state_name,
       st.shortform as state_shortform,
       co.name as country_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
LEFT JOIN states st ON e.state_id = st.id
LEFT JOIN countries co ON e.country_id = co.id
WHERE e.id = ?;

-- name: ListEnquiries :many
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       contact.email as contact_email,
       contact.mobile as contact_mobile,
       contact.direct_phone as contact_direct_phone,
       contact.phone as contact_phone,
       contact.phone_extension as contact_phone_extension,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
WHERE e.archived = 0
ORDER BY e.id DESC
LIMIT ? OFFSET ?;

-- name: ListArchivedEnquiries :many
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
WHERE e.archived = 1
ORDER BY e.id DESC
LIMIT ? OFFSET ?;

-- name: CreateEnquiry :execresult
INSERT INTO enquiries (
    created, title, user_id, customer_id, contact_id, contact_user_id,
    state_id, country_id, principle_id, status_id, comments,
    principle_code, gst, billing_address_id, shipping_address_id,
    posted, archived
) VALUES (
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);

-- name: UpdateEnquiry :exec
UPDATE enquiries 
SET title = ?, user_id = ?, customer_id = ?, contact_id = ?, contact_user_id = ?,
    state_id = ?, country_id = ?, principle_id = ?, status_id = ?, comments = ?,
    principle_code = ?, gst = ?, billing_address_id = ?, shipping_address_id = ?,
    posted = ?, submitted = ?
WHERE id = ?;

-- name: UpdateEnquiryStatus :exec
UPDATE enquiries SET status_id = ? WHERE id = ?;

-- name: ArchiveEnquiry :exec
UPDATE enquiries SET archived = 1 WHERE id = ?;

-- name: UnarchiveEnquiry :exec
UPDATE enquiries SET archived = 0 WHERE id = ?;

-- name: MarkEnquirySubmitted :exec
UPDATE enquiries SET submitted = ? WHERE id = ?;

-- name: SearchEnquiries :many
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
WHERE e.archived = 0
AND (
    e.title LIKE CONCAT('%', ?, '%') OR
    c.name LIKE CONCAT('%', ?, '%') OR
    CONCAT(contact.first_name, ' ', contact.last_name) LIKE CONCAT('%', ?, '%')
)
ORDER BY e.id DESC
LIMIT ? OFFSET ?;

-- name: GetEnquiriesByUser :many
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
WHERE e.user_id = ? AND e.archived = 0
ORDER BY e.id DESC
LIMIT ? OFFSET ?;

-- name: GetEnquiriesByCustomer :many
SELECT e.*, 
       u.first_name as user_first_name, 
       u.last_name as user_last_name,
       c.name as customer_name,
       contact.first_name as contact_first_name,
       contact.last_name as contact_last_name,
       s.name as status_name,
       p.name as principle_name,
       p.short_name as principle_short_name
FROM enquiries e
LEFT JOIN users u ON e.user_id = u.id
LEFT JOIN customers c ON e.customer_id = c.id
LEFT JOIN users contact ON e.contact_user_id = contact.id
LEFT JOIN statuses s ON e.status_id = s.id
LEFT JOIN principles p ON e.principle_id = p.id
WHERE e.customer_id = ? AND e.archived = 0
ORDER BY e.id DESC
LIMIT ? OFFSET ?;

-- name: CountEnquiries :one
SELECT COUNT(*) FROM enquiries WHERE archived = 0;

-- name: CountEnquiriesByStatus :one
SELECT COUNT(*) FROM enquiries WHERE status_id = ? AND archived = 0;

-- name: CountEnquiriesByPrinciple :one
SELECT COUNT(*) FROM enquiries WHERE principle_code = ?;

-- name: CountEnquiriesByPrincipleAndState :one
SELECT COUNT(*) FROM enquiries WHERE principle_code = ? AND state_id = ?;

-- name: GetAllStatuses :many
SELECT id, name FROM statuses ORDER BY name;

-- name: GetAllPrinciples :many
SELECT id, name, short_name, code FROM principles ORDER BY name;

-- name: GetAllStates :many
SELECT id, name, shortform, enqform FROM states ORDER BY name;

-- name: GetAllCountries :many
SELECT id, name FROM countries ORDER BY name;