-- name: GetExpiringSoonQuotes :many
SELECT 
  d.id AS document_id,
  u.username,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  q.date_issued,
  q.valid_until,
  COALESCE(qr_latest.reminder_type, 0) AS latest_reminder_type,
  COALESCE(qr_latest.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time

FROM quotes q
JOIN documents d ON d.id = q.document_id
JOIN users u ON u.id = d.user_id
JOIN enquiries e ON e.id = q.enquiry_id

LEFT JOIN (
    SELECT qr1.quote_id, qr1.reminder_type, qr1.date_sent
    FROM quote_reminders qr1
    JOIN (
        SELECT quote_id, MAX(reminder_type) AS max_type
        FROM quote_reminders
        GROUP BY quote_id
    ) qr2 ON qr1.quote_id = qr2.quote_id AND qr1.reminder_type = qr2.max_type
) qr_latest ON qr_latest.quote_id = d.id

WHERE 
  q.valid_until >= CURRENT_DATE 
  AND q.valid_until <= DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
  AND e.status_id = 5

ORDER BY q.valid_until;

-- name: GetExpiringSoonQuotesOnDay :many
SELECT 
  d.id AS document_id,
  u.username,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  q.date_issued,
  q.valid_until,
  COALESCE(qr_latest.reminder_type, 0) AS latest_reminder_type,
  COALESCE(qr_latest.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time

FROM quotes q
JOIN documents d ON d.id = q.document_id
JOIN users u ON u.id = d.user_id
JOIN enquiries e ON e.id = q.enquiry_id

LEFT JOIN (
    SELECT qr1.quote_id, qr1.reminder_type, qr1.date_sent
    FROM quote_reminders qr1
    JOIN (
        SELECT quote_id, MAX(reminder_type) AS max_type
        FROM quote_reminders
        GROUP BY quote_id
    ) qr2 ON qr1.quote_id = qr2.quote_id AND qr1.reminder_type = qr2.max_type
) qr_latest ON qr_latest.quote_id = d.id

WHERE 
  q.valid_until >= CURRENT_DATE 
  AND q.valid_until = DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
  AND e.status_id = 5

ORDER BY q.valid_until;

-- name: GetRecentlyExpiredQuotes :many
SELECT 
  d.id AS document_id,
  u.username,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  q.date_issued,
  q.valid_until,
  COALESCE(qr_latest.reminder_type, 0) AS latest_reminder_type,
  COALESCE(qr_latest.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time

FROM quotes q
JOIN documents d ON d.id = q.document_id
JOIN users u ON u.id = d.user_id
JOIN enquiries e ON e.id = q.enquiry_id

LEFT JOIN (
    SELECT qr1.quote_id, qr1.reminder_type, qr1.date_sent
    FROM quote_reminders qr1
    JOIN (
        SELECT quote_id, MAX(reminder_type) AS max_type
        FROM quote_reminders
        GROUP BY quote_id
    ) qr2 ON qr1.quote_id = qr2.quote_id AND qr1.reminder_type = qr2.max_type
) qr_latest ON qr_latest.quote_id = d.id

WHERE 
  q.valid_until < CURRENT_DATE 
  AND valid_until >= DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
  AND e.status_id = 5

ORDER BY q.valid_until DESC;

-- name: GetRecentlyExpiredQuotesOnDay :many
SELECT 
  d.id AS document_id,
  u.username,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  q.date_issued,
  q.valid_until,
  COALESCE(qr_latest.reminder_type, 0) AS latest_reminder_type,
  COALESCE(qr_latest.date_sent, CAST('1970-01-01 00:00:00' AS DATETIME)) AS latest_reminder_sent_time

FROM quotes q
JOIN documents d ON d.id = q.document_id
JOIN users u ON u.id = d.user_id
JOIN enquiries e ON e.id = q.enquiry_id

LEFT JOIN (
    SELECT qr1.quote_id, qr1.reminder_type, qr1.date_sent
    FROM quote_reminders qr1
    JOIN (
        SELECT quote_id, MAX(reminder_type) AS max_type
        FROM quote_reminders
        GROUP BY quote_id
    ) qr2 ON qr1.quote_id = qr2.quote_id AND qr1.reminder_type = qr2.max_type
) qr_latest ON qr_latest.quote_id = d.id

WHERE 
  q.valid_until < CURRENT_DATE 
  AND valid_until = DATE_SUB(CURRENT_DATE, INTERVAL ? DAY)
  AND e.status_id = 5

ORDER BY q.valid_until DESC;

-- name: GetQuoteRemindersByType :many
SELECT id, quote_id, reminder_type, date_sent, username
FROM quote_reminders
WHERE quote_id = ? AND reminder_type = ?
ORDER BY date_sent;

-- name: InsertQuoteReminder :execresult
INSERT INTO quote_reminders (quote_id, reminder_type, date_sent, username)
VALUES (?, ?, ?, ?);