-- name: GetExpiringSoonQuotes :many
WITH latest_revision AS (
  SELECT
    q.enquiry_id,
    MAX(d.revision) AS max_revision
  FROM quotes q
  JOIN documents d ON d.id = q.document_id
  GROUP BY q.enquiry_id
),
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,
  COALESCE(q.reminders_disabled, FALSE) AS reminders_disabled

FROM quotes q
JOIN documents d ON d.id = q.document_id
LEFT 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
JOIN latest_revision lr ON q.enquiry_id = lr.enquiry_id AND d.revision = lr.max_revision

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
  AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)

ORDER BY q.valid_until;

-- name: GetExpiringSoonQuotesOnDay :many
WITH latest_revision AS (
  SELECT
    q.enquiry_id,
    MAX(d.revision) AS max_revision
  FROM quotes q
  JOIN documents d ON d.id = q.document_id
  GROUP BY q.enquiry_id
),
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
LEFT 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
JOIN latest_revision lr ON q.enquiry_id = lr.enquiry_id AND d.revision = lr.max_revision

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 q.valid_until <= DATE_ADD(CURRENT_DATE, INTERVAL ? DAY)
  AND e.status_id = 5
  AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)

ORDER BY q.valid_until;

-- name: GetRecentlyExpiredQuotes :many
WITH latest_revision AS (
  SELECT
    q.enquiry_id,
    MAX(d.revision) AS max_revision
  FROM quotes q
  JOIN documents d ON d.id = q.document_id
  GROUP BY q.enquiry_id
),
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
LEFT 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
JOIN latest_revision lr ON q.enquiry_id = lr.enquiry_id AND d.revision = lr.max_revision

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
  AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)

ORDER BY q.valid_until DESC;

-- name: GetRecentlyExpiredQuotesOnDay :many
WITH latest_revision AS (
  SELECT
    q.enquiry_id,
    MAX(d.revision) AS max_revision
  FROM quotes q
  JOIN documents d ON d.id = q.document_id
  GROUP BY q.enquiry_id
),
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
LEFT 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
JOIN latest_revision lr ON q.enquiry_id = lr.enquiry_id AND d.revision = lr.max_revision

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
  AND (q.reminders_disabled IS NULL OR q.reminders_disabled = FALSE)

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 (?, ?, ?, ?);

-- name: DisableQuoteReminders :execresult
UPDATE quotes
SET reminders_disabled = TRUE,
    reminders_disabled_at = NOW(),
    reminders_disabled_by = ?
WHERE id = ?;

-- name: EnableQuoteReminders :execresult
UPDATE quotes
SET reminders_disabled = FALSE,
    reminders_disabled_at = NULL,
    reminders_disabled_by = NULL
WHERE id = ?;

-- name: GetQuoteRemindersDisabled :one
SELECT reminders_disabled, reminders_disabled_at, reminders_disabled_by
FROM quotes
WHERE id = ?;