set search_path TO bank;

create or replace procedure add_person (
  p_firstname text,
  p_lastname text,
  p_birthdate date
)
language plpgsql
as $$
declare
  v_holder_id bigint;
begin

    insert into holder(type) values ('PERSON')
    returning id into v_holder_id;

    insert into person(id, firstname, lastname, birthdate)
    values (v_holder_id, p_firstname, p_lastname, p_birthdate);

    raise notice 'Titulaire créé : % = % %',
      v_holder_id, p_firstname, p_lastname;
end;
$$;

create or replace procedure add_person (
  p_firstname text,
  p_lastname text,
  p_birthdate date,
  p_nb_account int
)
language plpgsql
as $$
declare
  v_holder_id bigint;
begin

    insert into holder(type) values ('PERSON')
    returning id into v_holder_id;

    insert into person(id, firstname, lastname, birthdate)
    values (v_holder_id, p_firstname, p_lastname, p_birthdate);

    for counter in 1..pb_nb_account loop
      call add_account(v_holder_id, 'EUR');
	end loop;

    raise notice 'Titulaire créé : % = % %',
      v_holder_id, p_firstname, p_lastname;
end;
$$;

create or replace procedure add_person (
  p_firstname text,
  p_lastname text,
  p_birthdate date,
  currencies text[]
)
language plpgsql
as $$
declare
  v_holder_id bigint;
begin

    insert into holder(type) values ('PERSON')
    returning id into v_holder_id;

    insert into person(id, firstname, lastname, birthdate)
    values (v_holder_id, p_firstname, p_lastname, p_birthdate);

    for counter in 1..array_length(currencies, 1) loop
      call add_account(v_holder_id, currencies[counter]);
	end loop;

    raise notice 'Titulaire créé : % = % %',
      v_holder_id, p_firstname, p_lastname;
end;
$$;

create or replace procedure add_bank (
  name text
)
language plpgsql
as $$
declare
  v_holder_id bigint;
begin

    insert into holder(type) values ('BANK')
    returning id into v_holder_id;

    insert into bank(id, name)
    values (v_holder_id, name);

    raise notice 'Titulaire créé : % = %',
      v_holder_id, name;
end;
$$;

create or replace procedure add_company (
 p_name text,
 p_registration_number text,
 p_created_at date
)
language plpgsql
as $$
declare
  v_holder_id bigint;
begin

    insert into holder(type) values ('COMPANY')
    returning id into v_holder_id;

    insert into company(id, name, registration_number, created_at)
    values (v_holder_id, p_name, p_registration_number, p_created_at);

    raise notice 'Titulaire créé : % = % %',
      v_holder_id, p_name, p_registration_number;
end;
$$;

create or replace procedure add_account(
  p_holder_id bigint,
  p_currency text
)
language plpgsql
as $$
declare
    v_account_id int;
begin

  insert into account(currency_code)
  values (p_currency)
  returning id into v_account_id;

  insert into account_holder (account_id, holder_id, share)
  values (v_account_id, p_holder_id, 1);

  raise notice 'Compte créé avec succès (ID=%)',
    v_account_id;
end;
$$;


create or replace procedure add_account(
    p_iban text,
    p_name text,
    p_holders int[],
    p_shares numeric[]
)
language plpgsql
as $$
declare
    v_account_id int;
    v_sum numeric(6,4) := 0;
    v_count int;
begin
    -- Vérification des tailles
    if array_length(p_holders, 1) is null or array_length(p_shares, 1) is null then
        raise exception 'Les tableaux de titulaires et de parts ne peuvent pas être vides.';
    end if;

    if array_length(p_holders, 1) <> array_length(p_shares, 1) then
        raise exception 'Les tableaux de titulaires et de parts doivent avoir la même taille.';
    end if;

    -- Calcul de la somme des parts
    -- select sum(unnest(p_shares)) into v_sum;
    for i in 1..array_length(p_shares, 1) loop
        v_sum := v_sum + p_shares[i];
    end loop;

    if abs(v_sum - 1.0) > 0.0001 then
        raise exception 'La somme des parts (%.4f) doit être égale à 1.0000', v_sum;
    end if;

    -- Vérification des titulaires
    select count(*) into v_count from holder where id = any(p_holders);
    if v_count <> array_length(p_holders, 1) then
        raise exception 'Un ou plusieurs titulaires n''existent pas.';
    end if;

    -- Création du compte
    insert into account(iban, name)
    values (p_iban, p_name)
    returning id into v_account_id;

    -- Association des titulaires
    for i in 1..array_length(p_holders, 1) loop
        insert into account_holder(account_id, holder_id, share)
        values (v_account_id, p_holders[i], p_shares[i]);
    end loop;

    raise notice 'Compte créé avec succès (ID=%) avec % titulaires.',
        v_account_id, array_length(p_holders, 1);
end;
$$;


create procedure transfer(
  p_source int,
  p_destination int,
  p_amount numeric,
  p_date date
)
language plpgsql
as $$
declare
  v_transaction_id int;
begin
  insert into transaction(amount, transaction_date)
    values (p_amount, p_date)
    returning id into v_transaction_id;

  insert into operation (transaction_id, account_id, amount, direction)
    values (v_transaction_id, p_source, p_amount, 'DEBIT');

  insert into operation (transaction_id, account_id, amount, direction)
    values (v_transaction_id, p_destination, p_amount, 'CREDIT');

  update account
    set balance = balance - p_amount
    where id = p_source;

  update account
    set balance = balance + p_amount
    WHERE id = p_destination;
end;
$$;
