-- Renewal views for fenedgec_members
-- Applies current requirements from requirements/renewal_views.md
-- Safe to run repeatedly: drops and recreates all renewal views.

-- Drop in dependency order.
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_accounts_with_contacts`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_public_contact`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_fen_contact`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_contact_2`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_contact_1`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_pending`;
DROP VIEW IF EXISTS `fenedgec_members`.`renewal_members`;

CREATE VIEW `fenedgec_members`.`renewal_members` AS
SELECT
  a.`id` AS `account_id`,
  a.`name` AS `account_name`,
  a.`account_no_of_members` AS `account_no_of_members`,
  a.`account_type_id` AS `account_type_id`,
  pat.`value` AS `account_type`,
  a.`sector_id` AS `sector_id`,
  ps.`value` AS `account_sector`,
  a.`public_location_id` AS `public_location_id`,
  ppl.`value` AS `public_location`,
  a.`facebook` AS `account_facebook`,
  a.`twitter` AS `account_twitter`,
  a.`other_social` AS `account_other_social`
FROM `fenedgec_members`.`accounts` a
LEFT JOIN `fenedgec_members`.`picklist_account_type` pat
  ON pat.`id` = a.`account_type_id`
LEFT JOIN `fenedgec_members`.`picklist_sector` ps
  ON ps.`id` = a.`sector_id`
LEFT JOIN `fenedgec_members`.`picklist_public_location` ppl
  ON ppl.`id` = a.`public_location_id`
WHERE a.`is_deleted` = 0
  AND LOWER(TRIM(COALESCE(pat.`slug`, ''))) = 'member';

CREATE VIEW `fenedgec_members`.`renewal_pending` AS
SELECT
  r.`id` AS `renewal_id`,
  r.`account_id` AS `renewal_account_id`,
  r.`renewal_year` AS `renewal_year`,
  r.`status` AS `renewal_status`,
  r.`selected` AS `renewal_selected`,
  r.`payment_method` AS `renewal_payment_method`,
  r.`payment_amount` AS `renewal_payment_amount`,
  r.`payment_date` AS `renewal_payment_date`,
  r.`created_at` AS `renewal_created_at`,
  r.`updated_at` AS `renewal_updated_at`
FROM `fenedgec_members`.`renewals` r
WHERE LOWER(TRIM(COALESCE(r.`status`, ''))) = 'pending';

CREATE VIEW `fenedgec_members`.`renewal_contact_1` AS
SELECT
  c.`id` AS `contact_1_contact_id`,
  c.`account_id` AS `contact_1_account_id`,
  TRIM(CONCAT_WS(' ', c.`first_name`, c.`last_name`)) AS `contact_1_name`,
  c.`title` AS `contact_1_position`,
  c.`contact_address_1` AS `contact_1_address_1`,
  c.`contact_address_2` AS `contact_1_address_2`,
  c.`contact_town` AS `contact_1_town`,
  c.`contact_county` AS `contact_1_county`,
  c.`contact_postcode` AS `contact_1_postcode`,
  c.`contact_email_1` AS `contact_1_email`,
  c.`phone_1` AS `contact_1_phone_1`,
  c.`phone_2` AS `contact_1_phone_2`
FROM `fenedgec_members`.`contacts` c
INNER JOIN (
  SELECT `account_id`, MIN(`id`) AS `min_id`
  FROM `fenedgec_members`.`contacts`
  WHERE `is_deleted` = 0
    AND `is_contact_1` = 1
  GROUP BY `account_id`
) x
  ON x.`account_id` = c.`account_id`
 AND x.`min_id` = c.`id`
WHERE c.`is_deleted` = 0
  AND c.`is_contact_1` = 1;

CREATE VIEW `fenedgec_members`.`renewal_contact_2` AS
SELECT
  c.`id` AS `contact_2_contact_id`,
  c.`account_id` AS `contact_2_account_id`,
  TRIM(CONCAT_WS(' ', c.`first_name`, c.`last_name`)) AS `contact_2_name`,
  c.`title` AS `contact_2_position`,
  c.`contact_address_1` AS `contact_2_address_1`,
  c.`contact_address_2` AS `contact_2_address_2`,
  c.`contact_town` AS `contact_2_town`,
  c.`contact_county` AS `contact_2_county`,
  c.`contact_postcode` AS `contact_2_postcode`,
  c.`contact_email_1` AS `contact_2_email`,
  c.`phone_1` AS `contact_2_phone_1`,
  c.`phone_2` AS `contact_2_phone_2`
FROM `fenedgec_members`.`contacts` c
INNER JOIN (
  SELECT `account_id`, MIN(`id`) AS `min_id`
  FROM `fenedgec_members`.`contacts`
  WHERE `is_deleted` = 0
    AND `is_contact_2` = 1
  GROUP BY `account_id`
) x
  ON x.`account_id` = c.`account_id`
 AND x.`min_id` = c.`id`
WHERE c.`is_deleted` = 0
  AND c.`is_contact_2` = 1;

CREATE VIEW `fenedgec_members`.`renewal_fen_contact` AS
SELECT
  c.`id` AS `fen_contact_contact_id`,
  c.`account_id` AS `fen_contact_account_id`,
  TRIM(CONCAT_WS(' ', c.`first_name`, c.`last_name`)) AS `fen_contact_name`,
  c.`contact_email_1` AS `fen_contact_email`
FROM `fenedgec_members`.`contacts` c
INNER JOIN (
  SELECT `account_id`, MIN(`id`) AS `min_id`
  FROM `fenedgec_members`.`contacts`
  WHERE `is_deleted` = 0
    AND `is_fen_1` = 1
  GROUP BY `account_id`
) x
  ON x.`account_id` = c.`account_id`
 AND x.`min_id` = c.`id`
WHERE c.`is_deleted` = 0
  AND c.`is_fen_1` = 1;

CREATE VIEW `fenedgec_members`.`renewal_public_contact` AS
SELECT
  c.`id` AS `public_contact_contact_id`,
  c.`account_id` AS `public_contact_account_id`,
  TRIM(CONCAT_WS(' ', c.`first_name`, c.`last_name`)) AS `public_contact_name`,
  c.`public_phone` AS `public_contact_public_phone`,
  c.`public_email` AS `public_contact_public_email`
FROM `fenedgec_members`.`contacts` c
INNER JOIN (
  SELECT `account_id`, MIN(`id`) AS `min_id`
  FROM `fenedgec_members`.`contacts`
  WHERE `is_deleted` = 0
    AND `is_public_contact` = 1
  GROUP BY `account_id`
) x
  ON x.`account_id` = c.`account_id`
 AND x.`min_id` = c.`id`
WHERE c.`is_deleted` = 0
  AND c.`is_public_contact` = 1;

CREATE VIEW `fenedgec_members`.`renewal_accounts_with_contacts` AS
SELECT
  rm.*,
  rp.*,
  rc1.`contact_1_contact_id`,
  rc1.`contact_1_account_id`,
  rc1.`contact_1_name`,
  rc1.`contact_1_position`,
  rc1.`contact_1_address_1`,
  rc1.`contact_1_address_2`,
  rc1.`contact_1_town`,
  rc1.`contact_1_county`,
  rc1.`contact_1_postcode`,
  rc1.`contact_1_email`,
  rc1.`contact_1_phone_1`,
  rc1.`contact_1_phone_2`,
  rc2.`contact_2_contact_id`,
  rc2.`contact_2_account_id`,
  rc2.`contact_2_name`,
  rc2.`contact_2_position`,
  rc2.`contact_2_address_1`,
  rc2.`contact_2_address_2`,
  rc2.`contact_2_town`,
  rc2.`contact_2_county`,
  rc2.`contact_2_postcode`,
  rc2.`contact_2_email`,
  rc2.`contact_2_phone_1`,
  rc2.`contact_2_phone_2`,
  rcf.`fen_contact_contact_id`,
  rcf.`fen_contact_account_id`,
  rcf.`fen_contact_name`,
  rcf.`fen_contact_email`,
  rpc.`public_contact_contact_id`,
  rpc.`public_contact_account_id`,
  rpc.`public_contact_name`,
  rpc.`public_contact_public_phone`,
  rpc.`public_contact_public_email`
FROM `fenedgec_members`.`renewal_pending` rp
INNER JOIN `fenedgec_members`.`renewal_members` rm
  ON rm.`account_id` = rp.`renewal_account_id`
LEFT JOIN `fenedgec_members`.`renewal_contact_1` rc1
  ON rc1.`contact_1_account_id` = rm.`account_id`
LEFT JOIN `fenedgec_members`.`renewal_contact_2` rc2
  ON rc2.`contact_2_account_id` = rm.`account_id`
LEFT JOIN `fenedgec_members`.`renewal_fen_contact` rcf
  ON rcf.`fen_contact_account_id` = rm.`account_id`
LEFT JOIN `fenedgec_members`.`renewal_public_contact` rpc
  ON rpc.`public_contact_account_id` = rm.`account_id`;
