-- Schema Analysis: Existing coupons table with basic structure
-- Integration Type: extension - adding missing columns for enhanced functionality
-- Dependencies: existing coupons table

-- Add missing columns to existing coupons table
ALTER TABLE public.coupons
ADD COLUMN image_url TEXT,
ADD COLUMN category TEXT,
ADD COLUMN is_active BOOLEAN DEFAULT true,
ADD COLUMN created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP;

-- Create enum for category validation
CREATE TYPE public.coupon_category AS ENUM (
  'restaurantes',
  'tiendas', 
  'servicios',
  'entretenimiento',
  'salud',
  'belleza'
);

-- Update category column to use enum with proper constraint
ALTER TABLE public.coupons
ALTER COLUMN category TYPE public.coupon_category USING category::public.coupon_category;

-- Add indexes for performance
CREATE INDEX idx_coupons_category ON public.coupons(category);
CREATE INDEX idx_coupons_is_active ON public.coupons(is_active);
CREATE INDEX idx_coupons_created_at ON public.coupons(created_at);
CREATE INDEX idx_coupons_valid_until ON public.coupons(valid_until);

-- Enable RLS if not already enabled
ALTER TABLE public.coupons ENABLE ROW LEVEL SECURITY;

-- RLS Policy: Public read access for active and valid coupons
CREATE POLICY "public_can_read_active_coupons"
ON public.coupons
FOR SELECT
TO public
USING (
  is_active = true 
  AND (valid_until IS NULL OR valid_until >= CURRENT_DATE)
);

-- Mock data with proper Spanish categories
DO $$
BEGIN
  -- Insert sample coupons with all required fields
  INSERT INTO public.coupons (
    business_name, title, description, valid_until, 
    image_url, category, is_active, created_at
  ) VALUES
  (
    'Restaurante El Buen Sabor',
    'Descuento del 20%',
    'Descuento válido en toda la carta. No aplica con otras promociones.',
    '2025-01-15'::date,
    'https://images.unsplash.com/photo-1647695822638-a40e238ddc39',
    'restaurantes'::public.coupon_category,
    true,
    '2024-10-15 10:00:00'::timestamptz
  ),
  (
    'Tienda La Moderna',
    'Descuento de $10',
    'Descuento en compras mayores a $50. Válido en toda la tienda.',
    '2024-12-31'::date,
    'https://images.unsplash.com/photo-1599190118801-8e4463c6a993',
    'tiendas'::public.coupon_category,
    true,
    '2024-10-10 14:30:00'::timestamptz
  ),
  (
    'Spa Relajación Total',
    'Descuento del 30% en masajes',
    'Descuento en todos los tipos de masajes. Incluye aromaterapia.',
    '2024-11-30'::date,
    'https://images.unsplash.com/photo-1706795033849-7ca391f007c5',
    'belleza'::public.coupon_category,
    true,
    '2024-10-05 09:15:00'::timestamptz
  ),
  (
    'Café Montaña',
    'Café gratis con postre',
    'Café americano gratis con la compra de cualquier postre.',
    '2024-10-25'::date,
    'https://images.unsplash.com/photo-1587299103717-ca5b2db07f47',
    'restaurantes'::public.coupon_category,
    true,
    '2024-10-12 11:20:00'::timestamptz
  ),
  (
    'Farmacia San José',
    'Descuento del 15% en medicamentos',
    'Descuento en medicamentos de venta libre y productos de cuidado personal.',
    '2024-12-15'::date,
    'https://images.unsplash.com/photo-1701117553039-d39c1ee3f3cd',
    'salud'::public.coupon_category,
    true,
    '2024-10-08 13:45:00'::timestamptz
  ),
  (
    'Cine Boquete',
    '2x1 en entradas',
    'Dos entradas por el precio de una. Válido de lunes a miércoles.',
    '2024-11-20'::date,
    'https://images.unsplash.com/photo-1669697243629-8c8a5d6dad9e',
    'entretenimiento'::public.coupon_category,
    true,
    '2024-10-01 08:30:00'::timestamptz
  );

EXCEPTION
  WHEN duplicate_object THEN
    RAISE NOTICE 'Objects already exist, skipping creation';
  WHEN OTHERS THEN
    RAISE NOTICE 'Migration error: %', SQLERRM;
END $$;