-- =============================================================================
-- MICHANGARRITO - 01 SCHEMAS
-- =============================================================================
-- Creación de schemas del sistema
-- =============================================================================

-- Schema de autenticación
CREATE SCHEMA IF NOT EXISTS auth;
COMMENT ON SCHEMA auth IS 'Autenticación y usuarios';

-- Schema de catálogo
CREATE SCHEMA IF NOT EXISTS catalog;
COMMENT ON SCHEMA catalog IS 'Productos y categorías';

-- Schema de ventas
CREATE SCHEMA IF NOT EXISTS sales;
COMMENT ON SCHEMA sales IS 'Ventas, pagos y cortes';

-- Schema de inventario
CREATE SCHEMA IF NOT EXISTS inventory;
COMMENT ON SCHEMA inventory IS 'Stock y movimientos';

-- Schema de clientes
CREATE SCHEMA IF NOT EXISTS customers;
COMMENT ON SCHEMA customers IS 'Clientes y fiados';

-- Schema de pedidos
CREATE SCHEMA IF NOT EXISTS orders;
COMMENT ON SCHEMA orders IS 'Pedidos y entregas';

-- Schema de suscripciones
CREATE SCHEMA IF NOT EXISTS subscriptions;
COMMENT ON SCHEMA subscriptions IS 'Planes y tokens IA';

-- Schema de mensajería
CREATE SCHEMA IF NOT EXISTS messaging;
COMMENT ON SCHEMA messaging IS 'WhatsApp y notificaciones';

-- Permisos
GRANT USAGE ON SCHEMA auth TO michangarrito_dev;
GRANT USAGE ON SCHEMA catalog TO michangarrito_dev;
GRANT USAGE ON SCHEMA sales TO michangarrito_dev;
GRANT USAGE ON SCHEMA inventory TO michangarrito_dev;
GRANT USAGE ON SCHEMA customers TO michangarrito_dev;
GRANT USAGE ON SCHEMA orders TO michangarrito_dev;
GRANT USAGE ON SCHEMA subscriptions TO michangarrito_dev;
GRANT USAGE ON SCHEMA messaging TO michangarrito_dev;

-- Schema de almacenamiento (MCH-029)
CREATE SCHEMA IF NOT EXISTS storage;
COMMENT ON SCHEMA storage IS 'Almacenamiento de archivos';

-- Schema de webhooks (MCH-029)
CREATE SCHEMA IF NOT EXISTS webhooks;
COMMENT ON SCHEMA webhooks IS 'Sistema de webhooks salientes';

-- Schema de auditoria (MCH-031)
CREATE SCHEMA IF NOT EXISTS audit;
COMMENT ON SCHEMA audit IS 'Logs de auditoria empresarial';

-- Schema de features (MCH-032)
CREATE SCHEMA IF NOT EXISTS features;
COMMENT ON SCHEMA features IS 'Feature flags por plan/tenant';

-- Schema de analytics (MCH-034, MCH-035)
CREATE SCHEMA IF NOT EXISTS analytics;
COMMENT ON SCHEMA analytics IS 'Metricas y reportes';

-- Permisos schemas base
GRANT USAGE ON SCHEMA storage TO michangarrito_dev;
GRANT USAGE ON SCHEMA webhooks TO michangarrito_dev;
GRANT USAGE ON SCHEMA audit TO michangarrito_dev;
GRANT USAGE ON SCHEMA features TO michangarrito_dev;
GRANT USAGE ON SCHEMA analytics TO michangarrito_dev;

-- Permisos en public
GRANT ALL ON SCHEMA public TO michangarrito_dev;
