/*
 * Restores legacy columns on eventhub.driver:
 * - card_nation
 * - card_number
 *
 * The script is conservative:
 * - it recreates the columns and the old check constraint
 * - it backfills values only when a driver has exactly one distinct card
 *   recoverable from:
 *     1. eventhub.driver_card.driver_id
 *     2. eventhub.event.driver_id + event.driver_card_id
 *
 * If a driver is associated with multiple distinct cards, the columns remain null.
 */

alter table eventhub.driver
    add column if not exists card_nation text;

alter table eventhub.driver
    add column if not exists card_number text;

do $restore$
begin
    if not exists (
        select 1
        from information_schema.table_constraints
        where constraint_schema = 'eventhub'
          and table_name = 'driver'
          and constraint_name = 'chk_driver_card_nation_when_number'
    ) then
        alter table eventhub.driver
            add constraint chk_driver_card_nation_when_number
                check (card_number is null or card_nation is not null);
    end if;
end
$restore$;

with direct_driver_cards as (
    select driver.id as driver_id,
           card.nation,
           card.card_number
    from eventhub.driver driver
    join eventhub.driver_card card
      on card.driver_id = driver.id
),
event_driver_cards as (
    select distinct event.driver_id,
           card.nation,
           card.card_number
    from eventhub.event event
    join eventhub.driver_card card
      on card.id = event.driver_card_id
    where event.driver_id is not null
      and event.driver_card_id is not null
),
all_driver_cards as (
    select driver_id, nation, card_number
    from direct_driver_cards
    union
    select driver_id, nation, card_number
    from event_driver_cards
),
single_card_per_driver as (
    select driver_id,
           max(nation) as card_nation,
           max(card_number) as card_number
    from all_driver_cards
    group by driver_id
    having count(*) = 1
),
updated_drivers as (
    update eventhub.driver driver
       set card_nation = single.card_nation,
           card_number = single.card_number,
           updated_at = now()
      from single_card_per_driver single
     where driver.id = single.driver_id
       and (
           driver.card_nation is distinct from single.card_nation
           or driver.card_number is distinct from single.card_number
       )
    returning driver.id
)
select count(*) as restored_driver_card_columns
from updated_drivers;

with ambiguous_drivers as (
    select driver_id,
           count(*) as distinct_card_count
    from (
        select distinct driver_id, nation, card_number
        from (
            select driver.id as driver_id, card.nation, card.card_number
            from eventhub.driver driver
            join eventhub.driver_card card
              on card.driver_id = driver.id
            union all
            select distinct event.driver_id, card.nation, card.card_number
            from eventhub.event event
            join eventhub.driver_card card
              on card.id = event.driver_card_id
            where event.driver_id is not null
              and event.driver_card_id is not null
        ) cards
    ) distinct_cards
    group by driver_id
    having count(*) > 1
)
select count(*) as ambiguous_driver_count
from ambiguous_drivers;
