--
-- This file is part of TALER
-- Copyright (C) 2025 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/>
--

-- @file example-statistics-0001.sql
-- @brief examples for how to add statistics to the merchant backend
-- @author Christian Grothoff

-- Everything in one big transaction
BEGIN;

-- Check patch versioning is in place.
SELECT _v.register_patch('example-statistics-0001', NULL, NULL);

CREATE SCHEMA example_statistics;

SET search_path TO example_statistics;


-- This is for now just an example for how to use the API. --- BEGIN EXAMPLE

-- Setup statistic: what do we want to track for 'deposits'?
-- (Note: this is basically the one "manual" step we might not keep hard-coded)
INSERT INTO merchant_statistic_bucket_meta
  (slug
  ,description
  ,stype
  ,ranges
  ,ages)
VALUES
  ('deposits'
  ,'sales (before refunds)'
  ,'amount'
  ,ARRAY['second'::merchant.statistic_range, 'minute', 'hour', 'day', 'month', 'quarter', 'year']
  ,ARRAY[120, 120, 48, 95, 36, 40, 100] -- track last 120 s, 120 minutes, 48 hours, 95 days, 36 months, 40 quarters & 10 years
  );

INSERT INTO merchant_statistic_interval_meta
  (slug
  ,description
  ,stype
  ,ranges
  ,precisions)
VALUES
  ('deposits'
  ,'sales (before refunds)'
  ,'amount'
  ,ARRAY[1,60, 24 * 60 * 60, 30 * 24 * 60 * 60, 365 * 24 * 60 * 60] -- second, minute, day, month, year
  ,ARRAY[1,1, 60, 60 * 60, 24 * 60 * 60] -- second, second, minute, hour, day
  );


-- This is just another example for how to use the API. -- BEGIN EXAMPLE

-- Setup statistic
-- (Note: this is basically the one "manual" step we might not keep hard-coded)
INSERT INTO merchant_statistic_bucket_meta
  (slug
  ,description
  ,stype
  ,ranges
  ,ages)
VALUES
  ('products-sold'
  ,'products sold (only those tracked in inventory)'
  ,'number'
  ,ARRAY['second'::merchant.statistic_range, 'minute' 'day', 'week', 'month', 'quarter', 'year']
  ,ARRAY[120, 120, 60, 12, 24, 8, 10] -- track last 120s, 120 minutes, 60 days, 12 weeks, 24 months, 8 quarters and 10 years
  );

CREATE OR REPLACE FUNCTION merchant_products_sold_statistics_trigger()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
  my_sold INT8;
BEGIN
  my_sold = NEW.total_sold - OLD.total_sold;
  IF (0 < my_sold)
  THEN
    CALL merchant_do_bump_number_stat
      ('products-sold'
      ,CURRENT_TIMESTAMP(0)::TIMESTAMP
      ,my_sold);
  END IF;
  RETURN NEW;
END $$;

-- Whenever inventory changes, call our trigger to bump statistics
CREATE TRIGGER merchant_products_on_sold
  AFTER UPDATE
     ON merchant_inventory
  FOR EACH ROW EXECUTE FUNCTION merchant_products_sold_statistics_trigger();

-- This is for now just an example for how to use the API. --- END EXAMPLE

COMMIT;
