-- name: GetExpiringSoonQuotes :many
WITH ranked_reminders AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username,
    ROW_NUMBER() OVER (
      PARTITION BY quote_id
      ORDER BY reminder_type DESC, date_sent DESC
    ) AS rn
  FROM quote_reminders
),
latest_quote_reminder AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username
  FROM ranked_reminders
  WHERE rn = 1
)

SELECT 
  d.id AS document_id,
  u.username,
  u.email as user_email,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  uu.first_name as customer_name,
  uu.email as customer_email,
  q.date_issued,
  q.valid_until,
  COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
  COALESCE(lqr.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
JOIN users uu ON uu.id = e.contact_user_id

LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_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
WITH ranked_reminders AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username,
    ROW_NUMBER() OVER (
      PARTITION BY quote_id
      ORDER BY reminder_type DESC, date_sent DESC
    ) AS rn
  FROM quote_reminders
),
latest_quote_reminder AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username
  FROM ranked_reminders
  WHERE rn = 1
)

SELECT 
  d.id AS document_id,
  u.username,
  u.email as user_email,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  uu.first_name as customer_name,
  uu.email as customer_email,
  q.date_issued,
  q.valid_until,
  COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
  COALESCE(lqr.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
JOIN users uu ON uu.id = e.contact_user_id

LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_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
WITH ranked_reminders AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username,
    ROW_NUMBER() OVER (
      PARTITION BY quote_id
      ORDER BY reminder_type DESC, date_sent DESC
    ) AS rn
  FROM quote_reminders
),
latest_quote_reminder AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username
  FROM ranked_reminders
  WHERE rn = 1
)

SELECT 
  d.id AS document_id,
  u.username,
  u.email as user_email,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  uu.first_name as customer_name,
  uu.email as customer_email,
  q.date_issued,
  q.valid_until,
  COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
  COALESCE(lqr.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
JOIN users uu ON uu.id = e.contact_user_id

LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_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
WITH ranked_reminders AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username,
    ROW_NUMBER() OVER (
      PARTITION BY quote_id
      ORDER BY reminder_type DESC, date_sent DESC
    ) AS rn
  FROM quote_reminders
),
latest_quote_reminder AS (
  SELECT
    id,
    quote_id,
    reminder_type,
    date_sent,
    username
  FROM ranked_reminders
  WHERE rn = 1
)

SELECT 
  d.id AS document_id,
  u.username,
  u.email as user_email,
  e.id AS enquiry_id,
  e.title as enquiry_ref,
  uu.first_name as customer_name,
  uu.email as customer_email,
  q.date_issued,
  q.valid_until,
  COALESCE(lqr.reminder_type, 0) AS latest_reminder_type,
  COALESCE(lqr.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
JOIN users uu ON uu.id = e.contact_user_id 

LEFT JOIN latest_quote_reminder lqr on d.id = lqr.quote_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 (?, ?, ?, ?);