-- Catálogo global de métodos de pago por país (gestionado por platform admin)
CREATE TABLE public.country_payment_methods (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  country_code text NOT NULL,
  method_code  text NOT NULL,
  label        text NOT NULL,
  kind         text NOT NULL,
  description  text,
  recommended  boolean NOT NULL DEFAULT false,
  sort_order   integer NOT NULL DEFAULT 0,
  is_active    boolean NOT NULL DEFAULT true,
  created_at   timestamptz NOT NULL DEFAULT now(),
  updated_at   timestamptz NOT NULL DEFAULT now(),
  CONSTRAINT country_payment_methods_country_method_uk UNIQUE (country_code, method_code)
);

GRANT SELECT ON public.country_payment_methods TO authenticated;
GRANT ALL    ON public.country_payment_methods TO service_role;

ALTER TABLE public.country_payment_methods ENABLE ROW LEVEL SECURITY;

CREATE POLICY "country_payment_methods_read_authenticated"
  ON public.country_payment_methods FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "country_payment_methods_insert_platform_admin"
  ON public.country_payment_methods FOR INSERT
  TO authenticated
  WITH CHECK (public.current_user_is_platform_admin());

CREATE POLICY "country_payment_methods_update_platform_admin"
  ON public.country_payment_methods FOR UPDATE
  TO authenticated
  USING (public.current_user_is_platform_admin())
  WITH CHECK (public.current_user_is_platform_admin());

CREATE POLICY "country_payment_methods_delete_platform_admin"
  ON public.country_payment_methods FOR DELETE
  TO authenticated
  USING (public.current_user_is_platform_admin());

CREATE TRIGGER country_payment_methods_touch_updated_at
  BEFORE UPDATE ON public.country_payment_methods
  FOR EACH ROW EXECUTE FUNCTION public.touch_updated_at();

-- Seed inicial desde el catálogo del frontend (idempotente)
INSERT INTO public.country_payment_methods (country_code, method_code, label, kind, recommended, sort_order)
VALUES
('AR', 'ar-mercado-pago', 'Mercado Pago', 'wallet', true, 0),
('AR', 'ar-modo', 'MODO', 'wallet', true, 1),
('AR', 'ar-cuenta-dni', 'Cuenta DNI', 'wallet', false, 2),
('AR', 'ar-qr-interoperable', 'QR interoperable', 'qr', true, 3),
('AR', 'ar-transferencia-30', 'Transferencia 3.0', 'instant_transfer', false, 4),
('AR', 'ar-uala', 'Ualá', 'wallet', false, 5),
('AR', 'ar-naranja-x', 'Naranja X', 'wallet', false, 6),
('AR', 'ar-pago-facil', 'Pago Fácil', 'voucher', false, 7),
('AR', 'ar-rapipago', 'Rapipago', 'voucher', false, 8),
('BO', 'bo-qr-simple', 'QR Simple', 'qr', true, 0),
('BO', 'bo-simple', 'Simple', 'wallet', true, 1),
('BO', 'bo-tigo-money', 'Tigo Money', 'wallet', false, 2),
('BO', 'bo-soli', 'Soli', 'wallet', false, 3),
('BO', 'bo-pagofacil', 'PagoFácil Bolivia', 'voucher', false, 4),
('BR', 'br-pix', 'Pix', 'instant_transfer', true, 0),
('BR', 'br-pix-qr', 'Pix QR', 'qr', true, 1),
('BR', 'br-boleto', 'Boleto Bancário', 'voucher', false, 2),
('BR', 'br-picpay', 'PicPay', 'wallet', false, 3),
('BR', 'br-mercado-pago', 'Mercado Pago', 'wallet', false, 4),
('BR', 'br-nubank', 'Nubank', 'wallet', false, 5),
('BR', 'br-ted-doc', 'TED/DOC', 'bank_transfer', false, 6),
('CL', 'cl-redcompra', 'Redcompra', 'card', true, 0),
('CL', 'cl-webpay', 'Webpay', 'card', true, 1),
('CL', 'cl-mach', 'MACH', 'wallet', true, 2),
('CL', 'cl-mercado-pago', 'Mercado Pago', 'wallet', false, 3),
('CL', 'cl-servipag', 'Servipag', 'voucher', false, 4),
('CL', 'cl-khipu', 'Khipu', 'bank_transfer', false, 5),
('CL', 'cl-fpay', 'Fpay', 'wallet', false, 6),
('CO', 'co-pse', 'PSE', 'bank_transfer', true, 0),
('CO', 'co-nequi', 'Nequi', 'wallet', true, 1),
('CO', 'co-nequi-qr', 'Nequi QR', 'qr', true, 2),
('CO', 'co-daviplata', 'Daviplata', 'wallet', true, 3),
('CO', 'co-bre-b', 'Bre-B', 'instant_transfer', false, 4),
('CO', 'co-bancolombia', 'Bancolombia', 'bank_transfer', false, 5),
('CO', 'co-efecty', 'Efecty', 'voucher', false, 6),
('CO', 'co-mercado-pago', 'Mercado Pago', 'wallet', false, 7),
('CO', 'co-addi', 'Addi', 'bnpl', false, 8),
('EC', 'ec-deuna', 'DeUna!', 'wallet', true, 0),
('EC', 'ec-payphone', 'PayPhone', 'wallet', true, 1),
('EC', 'ec-transferencia', 'Transferencia interbancaria', 'bank_transfer', false, 2),
('EC', 'ec-datafast', 'Datafast', 'card', false, 3),
('EC', 'ec-medianet', 'Medianet', 'card', false, 4),
('EC', 'ec-kushki', 'Kushki', 'other', false, 5),
('GY', 'gy-mmg', 'MMG+', 'wallet', true, 0),
('GY', 'gy-mobile-money', 'Mobile Money Guyana', 'wallet', false, 1),
('GY', 'gy-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('PY', 'py-bancard-qr', 'QR Bancard', 'qr', true, 0),
('PY', 'py-tigo-money', 'Tigo Money', 'wallet', true, 1),
('PY', 'py-personal-pay', 'Personal Pay', 'wallet', false, 2),
('PY', 'py-aqui-pago', 'Aquí Pago', 'voucher', false, 3),
('PY', 'py-pago-express', 'Pago Express', 'voucher', false, 4),
('PE', 'pe-yape', 'Yape', 'wallet', true, 0),
('PE', 'pe-plin', 'Plin', 'wallet', true, 1),
('PE', 'pe-pagoefectivo', 'PagoEfectivo', 'voucher', false, 2),
('PE', 'pe-izipay', 'Izipay', 'card', false, 3),
('PE', 'pe-niubiz', 'Niubiz', 'card', false, 4),
('PE', 'pe-tunki', 'Tunki', 'wallet', false, 5),
('PE', 'pe-transferencia', 'Transferencia interbancaria', 'bank_transfer', false, 6),
('SR', 'sr-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 0),
('SR', 'sr-mobile-banking', 'Banca móvil', 'wallet', false, 1),
('SR', 'sr-qr', 'Pago QR', 'qr', false, 2),
('UY', 'uy-mercado-pago', 'Mercado Pago', 'wallet', true, 0),
('UY', 'uy-prex', 'Prex', 'wallet', false, 1),
('UY', 'uy-oca-blue', 'OCA Blue', 'wallet', false, 2),
('UY', 'uy-midinero', 'MiDinero', 'wallet', false, 3),
('UY', 'uy-abitab', 'Abitab', 'voucher', false, 4),
('UY', 'uy-redpagos', 'RedPagos', 'voucher', false, 5),
('VE', 've-pago-movil', 'Pago móvil interbancario', 'instant_transfer', true, 0),
('VE', 've-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 1),
('VE', 've-zelle', 'Zelle', 'bank_transfer', false, 2),
('VE', 've-usdt', 'USDT', 'crypto', false, 3),
('VE', 've-binance-pay', 'Binance Pay', 'crypto', false, 4),
('BZ', 'bz-e-kyash', 'E-Kyash', 'wallet', true, 0),
('BZ', 'bz-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('BZ', 'bz-mobile-pay', 'Pago móvil local', 'wallet', false, 2),
('CR', 'cr-sinpe-movil', 'SINPE Móvil', 'instant_transfer', true, 0),
('CR', 'cr-sinpe', 'Transferencia SINPE', 'bank_transfer', false, 1),
('CR', 'cr-bn-servicios', 'BN Servicios', 'voucher', false, 2),
('CR', 'cr-wink', 'Wink', 'wallet', false, 3),
('CR', 'cr-kash', 'Kash', 'wallet', false, 4),
('SV', 'sv-chivo', 'Chivo Wallet', 'wallet', true, 0),
('SV', 'sv-bitcoin-lightning', 'Bitcoin Lightning', 'crypto', false, 1),
('SV', 'sv-tigo-money', 'Tigo Money', 'wallet', false, 2),
('SV', 'sv-n1co', 'N1CO', 'wallet', false, 3),
('SV', 'sv-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 4),
('GT', 'gt-tigo-money', 'Tigo Money', 'wallet', true, 0),
('GT', 'gt-paggo', 'Paggo', 'wallet', false, 1),
('GT', 'gt-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 2),
('GT', 'gt-bac-link', 'BAC Compra Click', 'other', false, 3),
('HN', 'hn-tigo-money', 'Tigo Money', 'wallet', true, 0),
('HN', 'hn-tengo', 'Tengo', 'wallet', false, 1),
('HN', 'hn-kash', 'Kash', 'wallet', false, 2),
('HN', 'hn-bac-link', 'BAC Compra Click', 'other', false, 3),
('HN', 'hn-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 4),
('MX', 'mx-spei', 'SPEI', 'instant_transfer', true, 0),
('MX', 'mx-codi', 'CoDi', 'qr', true, 1),
('MX', 'mx-oxxo-pay', 'OXXO Pay', 'voucher', false, 2),
('MX', 'mx-mercado-pago', 'Mercado Pago', 'wallet', false, 3),
('MX', 'mx-kueski-pay', 'Kueski Pay', 'bnpl', false, 4),
('MX', 'mx-clip', 'Clip', 'card', false, 5),
('MX', 'mx-paypal', 'PayPal', 'wallet', false, 6),
('NI', 'ni-transferencia', 'Transferencia bancaria', 'bank_transfer', true, 0),
('NI', 'ni-pago-movil', 'Pago móvil bancario', 'wallet', false, 1),
('NI', 'ni-bac-link', 'BAC Compra Click', 'other', false, 2),
('NI', 'ni-billetera-local', 'Billetera móvil local', 'wallet', false, 3),
('PA', 'pa-yappy', 'Yappy', 'wallet', true, 0),
('PA', 'pa-nequi', 'Nequi Panamá', 'wallet', false, 1),
('PA', 'pa-ach', 'ACH', 'bank_transfer', false, 2),
('PA', 'pa-ach-xpress', 'ACH Xpress', 'instant_transfer', false, 3),
('PA', 'pa-punto-pago', 'Punto Pago', 'voucher', false, 4),
('PA', 'pa-clave', 'Clave', 'card', false, 5),
('AG', 'ag-dcash', 'DCash', 'wallet', false, 0),
('AG', 'ag-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('AG', 'ag-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('BS', 'bs-sand-dollar', 'Sand Dollar', 'wallet', true, 0),
('BS', 'bs-kanoo', 'Kanoo', 'wallet', false, 1),
('BS', 'bs-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('BB', 'bb-mmoney', 'mMoney', 'wallet', false, 0),
('BB', 'bb-surepay', 'SurePay', 'voucher', false, 1),
('BB', 'bb-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('CU', 'cu-transfermovil', 'Transfermóvil', 'wallet', true, 0),
('CU', 'cu-enzona', 'EnZona', 'wallet', true, 1),
('CU', 'cu-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 2),
('DM', 'dm-dcash', 'DCash', 'wallet', false, 0),
('DM', 'dm-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('DM', 'dm-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('GD', 'gd-dcash', 'DCash', 'wallet', false, 0),
('GD', 'gd-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('GD', 'gd-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('HT', 'ht-moncash', 'MonCash', 'wallet', true, 0),
('HT', 'ht-natcash', 'Natcash', 'wallet', false, 1),
('HT', 'ht-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('JM', 'jm-lynk', 'Lynk', 'wallet', true, 0),
('JM', 'jm-ncb-quisk', 'NCB Quisk', 'wallet', false, 1),
('JM', 'jm-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('PR', 'pr-ath-movil', 'ATH Móvil', 'wallet', true, 0),
('PR', 'pr-paypal', 'PayPal', 'wallet', false, 1),
('PR', 'pr-venmo', 'Venmo', 'wallet', false, 2),
('PR', 'pr-apple-pay', 'Apple Pay', 'wallet', false, 3),
('PR', 'pr-google-pay', 'Google Pay', 'wallet', false, 4),
('DO', 'do-azul', 'Azul', 'card', true, 0),
('DO', 'do-cardnet', 'CardNet', 'card', false, 1),
('DO', 'do-tpago', 'tPago', 'wallet', false, 2),
('DO', 'do-qik', 'Qik', 'wallet', false, 3),
('DO', 'do-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 4),
('KN', 'kn-dcash', 'DCash', 'wallet', false, 0),
('KN', 'kn-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('KN', 'kn-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('VC', 'vc-dcash', 'DCash', 'wallet', false, 0),
('VC', 'vc-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('VC', 'vc-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('LC', 'lc-dcash', 'DCash', 'wallet', false, 0),
('LC', 'lc-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 1),
('LC', 'lc-mobile-wallet', 'Billetera móvil local', 'wallet', false, 2),
('TT', 'tt-wipay', 'WiPay', 'wallet', true, 0),
('TT', 'tt-paywise', 'PayWise', 'voucher', false, 1),
('TT', 'tt-bank-transfer', 'Transferencia bancaria local', 'bank_transfer', false, 2),
('TT', 'tt-endcash', 'Endcash', 'wallet', false, 3),
('GQ', 'gq-orange-money', 'Orange Money', 'wallet', false, 0),
('GQ', 'gq-muni', 'Muni', 'wallet', false, 1),
('GQ', 'gq-mobile-money-cemac', 'Mobile Money CEMAC', 'wallet', false, 2),
('GQ', 'gq-transferencia', 'Transferencia bancaria', 'bank_transfer', false, 3)
ON CONFLICT (country_code, method_code) DO NOTHING;