--
-- This file is part of TALER
-- Copyright (C) 2026 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

DROP FUNCTION IF EXISTS merchant_do_activate_account;
CREATE FUNCTION merchant_do_activate_account(
   IN in_h_wire BYTEA
  ,IN in_salt BYTEA
  ,IN in_full_payto TEXT
  ,IN in_credit_facade_url TEXT -- can be NULL
  ,IN in_credit_facade_credentials TEXT -- can be NULL
  ,IN in_extra_wire_subject_metadata TEXT -- can be NULL
  ,OUT out_h_wire BYTEA
  ,OUT out_salt BYTEA
  ,OUT out_not_found BOOL
  ,OUT out_conflict BOOL
)
LANGUAGE plpgsql
AS $$
DECLARE my_active BOOL;
DECLARE my_cfu TEXT;
DECLARE my_cfc TEXT;
DECLARE my_ewsm TEXT;
DECLARE my_h_wire BYTEA;
DECLARE my_salt BYTEA;
BEGIN
  out_not_found = FALSE;
  out_conflict = FALSE;
  out_h_wire = in_h_wire;
  out_salt = in_salt;

  INSERT INTO merchant_accounts
    AS ma
    (h_wire
    ,salt
    ,payto_uri
    ,credit_facade_url
    ,credit_facade_credentials
    ,active
    ,extra_wire_subject_metadata
    ) VALUES (
      in_h_wire
     ,in_salt
     ,in_full_payto
     ,in_credit_facade_url
     ,in_credit_facade_credentials::JSONB
     ,TRUE
     ,in_extra_wire_subject_metadata
   ) ON CONFLICT DO NOTHING;
  IF FOUND
  THEN
    -- Notify taler-merchant-kyccheck about the change in
    -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
    NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG;
    RETURN;
  END IF;

  SELECT h_wire
        ,salt
        ,active
        ,credit_facade_url
        ,credit_facade_credentials::TEXT
        ,extra_wire_subject_metadata
    INTO my_h_wire
        ,my_salt
        ,my_active
        ,my_cfu
        ,my_cfc
        ,my_ewsm
    FROM merchant_accounts
   WHERE payto_uri=in_full_payto;
  IF NOT FOUND
  THEN
    -- This should never happen (we had a conflict!)
    -- Still, safe way is to return not found.
    out_not_found = TRUE;
    RETURN;
  END IF;

  -- Check for conflict
  IF (my_active AND
      (ROW (my_cfu
           ,my_cfc
           ,my_ewsm)
       IS DISTINCT FROM
       ROW (in_credit_facade_url
           ,in_credit_facade_credentials
           ,in_extra_wire_subject_metadata)))
  THEN
    -- Active conflicting account, refuse!
    out_conflict = TRUE;
    RETURN;
  END IF;

  -- Equivalent account exists, use its salt instead of the new salt
  -- and just set it to active!
  out_salt = my_salt;
  out_h_wire = my_h_wire;

  -- Now check if existing account is already active
  IF my_active
  THEN
    -- nothing to do
    RETURN;
  END IF;

  UPDATE merchant_accounts
     SET active=TRUE
        ,credit_facade_url=in_credit_facade_url
        ,credit_facade_credentials=in_credit_facade_credentials::JSONB
        ,extra_wire_subject_metadata=in_extra_wire_subject_metadata
   WHERE h_wire=out_h_wire;

  -- Notify taler-merchant-kyccheck about the change in (active)
  -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
  NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG;

END $$;
