-- Affiliate commission + account credit (table prefix UOYP7Y_). Run once in phpMyAdmin, top to bottom.
-- Same tables the C2P Affiliate plugin creates on activation; use this on the live site
-- if the plugin is already active (WordPress only runs the activation step when it's switched on).
-- If a step says "Duplicate column" or "Table already exists", it's already done — skip it.

-- 1. Referral codes: count how often each link is opened (the table already exists).
ALTER TABLE UOYP7Y_c2p_affiliate_links
  ADD COLUMN clicks INT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id;

-- 2. Account credit ledger. The balance is the sum of `amount`
--    (+ converted commission / given back / added by staff, − spent on orders).
CREATE TABLE IF NOT EXISTS UOYP7Y_c2p_affiliate_credits_details (
  id                 BIGINT(20)    NOT NULL AUTO_INCREMENT,
  user_id            BIGINT(20)    NOT NULL,
  customer_id        BIGINT(20)    NOT NULL DEFAULT 0,
  amount             DECIMAL(10,2) NOT NULL,
  transaction_type   VARCHAR(50)   NOT NULL,
  reference_order_id BIGINT(20)    NOT NULL DEFAULT 0,
  description        TEXT,
  created_by         BIGINT(20)    NOT NULL DEFAULT 0,
  created_at         DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY user_id (user_id),
  KEY customer_id (customer_id),
  KEY reference_order_id (reference_order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

-- 2b. Only if the credit table already existed before (from the first version of the plugin):
--     who made each change. Skip if step 2 just created the table.
-- ALTER TABLE UOYP7Y_c2p_affiliate_credits_details ADD COLUMN created_by BIGINT(20) NOT NULL DEFAULT 0 AFTER description;

-- 3. Commission: one row per referred order.
CREATE TABLE IF NOT EXISTS UOYP7Y_c2p_affiliate_commissions (
  id            BIGINT(20)    NOT NULL AUTO_INCREMENT,
  affiliate_id  BIGINT(20)    NOT NULL,
  customer_id   BIGINT(20)    NOT NULL,
  order_id      BIGINT(20)    NOT NULL,
  order_total   DECIMAL(10,2) NOT NULL DEFAULT 0,
  rate          DECIMAL(5,2)  NOT NULL DEFAULT 0,
  amount        DECIMAL(10,2) NOT NULL DEFAULT 0,
  status        VARCHAR(20)   NOT NULL DEFAULT 'pending',
  note          VARCHAR(255)  NOT NULL DEFAULT '',
  completed_at  DATETIME      NULL,
  available_at  DATETIME      NULL,
  converted_at  DATETIME      NULL,
  credit_id     BIGINT(20)    NOT NULL DEFAULT 0,
  created_at    DATETIME      NOT NULL,
  updated_at    DATETIME      NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY order_id (order_id),
  KEY affiliate_status (affiliate_id, status),
  KEY status_available (status, available_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

-- 4. Settings default (only if never saved). Turn it on in WooCommerce → C2P Affiliate → Settings.
INSERT IGNORE INTO UOYP7Y_options (option_name, option_value, autoload)
VALUES ('_c2p_affliat_main_settings', 'a:5:{s:7:"enabled";s:1:"0";s:4:"rate";s:1:"5";s:10:"max_orders";s:1:"3";s:9:"hold_days";s:2:"30";s:11:"cookie_days";s:2:"30";}', 'yes');
