-- =============================================================================
-- MC Cars - Admin portal rework: pricing snapshot columns on leads,
-- updated create_lead RPC to accept pricing params.
-- Idempotent.
-- =============================================================================

-- -----------------------------------------------------------------------------
-- 1. Add pricing snapshot columns to leads
-- -----------------------------------------------------------------------------
alter table public.leads add column if not exists daily_subtotal      integer not null default 0;
alter table public.leads add column if not exists weekend_subtotal    integer not null default 0;
alter table public.leads add column if not exists subtotal_eur        integer not null default 0;
alter table public.leads add column if not exists vat_eur             integer not null default 0;
alter table public.leads add column if not exists total_eur           integer not null default 0;
alter table public.leads add column if not exists deposit_eur         integer not null default 0;
alter table public.leads add column if not exists total_days          integer not null default 0;
alter table public.leads add column if not exists weekday_count       integer not null default 0;
alter table public.leads add column if not exists weekend_day_count   integer not null default 0;

-- -----------------------------------------------------------------------------
-- 2. Update create_lead RPC to accept and store pricing snapshot
-- -----------------------------------------------------------------------------
create or replace function public.create_lead(
    p_name text,
    p_email text,
    p_phone text default '',
    p_vehicle_id uuid default null,
    p_vehicle_label text default '',
    p_date_from date default null,
    p_date_to date default null,
    p_message text default '',
    p_source text default 'website',
    p_daily_subtotal integer default 0,
    p_weekend_subtotal integer default 0,
    p_subtotal_eur integer default 0,
    p_vat_eur integer default 0,
    p_total_eur integer default 0,
    p_deposit_eur integer default 0,
    p_total_days integer default 0,
    p_weekday_count integer default 0,
    p_weekend_day_count integer default 0
)
returns uuid
language plpgsql
security definer
set search_path = public
as $$
declare
    v_id uuid;
begin
    insert into public.leads (
        name,
        email,
        phone,
        vehicle_id,
        vehicle_label,
        date_from,
        date_to,
        message,
        source,
        daily_subtotal,
        weekend_subtotal,
        subtotal_eur,
        vat_eur,
        total_eur,
        deposit_eur,
        total_days,
        weekday_count,
        weekend_day_count
    )
    values (
        p_name,
        p_email,
        coalesce(p_phone, ''),
        p_vehicle_id,
        coalesce(p_vehicle_label, ''),
        p_date_from,
        p_date_to,
        coalesce(p_message, ''),
        coalesce(p_source, 'website'),
        coalesce(p_daily_subtotal, 0),
        coalesce(p_weekend_subtotal, 0),
        coalesce(p_subtotal_eur, 0),
        coalesce(p_vat_eur, 0),
        coalesce(p_total_eur, 0),
        coalesce(p_deposit_eur, 0),
        coalesce(p_total_days, 0),
        coalesce(p_weekday_count, 0),
        coalesce(p_weekend_day_count, 0)
    )
    returning id into v_id;

    return v_id;
end;
$$;

revoke all on function public.create_lead(
    text, text, text, uuid, text, date, date, text, text,
    integer, integer, integer, integer, integer, integer, integer, integer, integer
) from public;
grant execute on function public.create_lead(
    text, text, text, uuid, text, date, date, text, text,
    integer, integer, integer, integer, integer, integer, integer, integer, integer
) to anon, authenticated, service_role;

notify pgrst, 'reload schema';
