-- Database Performance Optimization Indexes
-- Run this directly on PostgreSQL or use: npx prisma db execute --stdin < DATABASE_OPTIMIZATION_MIGRATION.sql

-- Order indexes
CREATE INDEX IF NOT EXISTS idx_order_customer_created ON "Order"("customerId", "createdAt" DESC);
CREATE INDEX IF NOT EXISTS idx_order_status_created ON "Order"("status", "createdAt" DESC);
CREATE INDEX IF NOT EXISTS idx_order_email ON "Order"("email");

-- OrderItem indexes
CREATE INDEX IF NOT EXISTS idx_order_item_order_product ON "OrderItem"("orderId", "productId");
CREATE INDEX IF NOT EXISTS idx_order_item_product ON "OrderItem"("productId");

-- DesignApproval indexes
CREATE INDEX IF NOT EXISTS idx_design_approval_email_status ON "DesignApproval"("customerEmail", "status");
CREATE INDEX IF NOT EXISTS idx_design_approval_status_created ON "DesignApproval"("status", "createdAt" DESC);
CREATE INDEX IF NOT EXISTS idx_design_approval_product ON "DesignApproval"("productId");

-- Review indexes
CREATE INDEX IF NOT EXISTS idx_review_product_approved ON "Review"("productId", "approved");
CREATE INDEX IF NOT EXISTS idx_review_customer ON "Review"("customerId");

-- Product indexes
CREATE INDEX IF NOT EXISTS idx_product_category_visible ON "Product"("category", "showOnProducts");
CREATE INDEX IF NOT EXISTS idx_product_personalised ON "Product"("showOnPersonalised");

-- DesignProof indexes
CREATE INDEX IF NOT EXISTS idx_design_proof_status ON "DesignProof"("status", "createdAt" DESC);

-- Message indexes
CREATE INDEX IF NOT EXISTS idx_message_proof ON "Message"("proofId", "createdAt");

-- Customer indexes
CREATE INDEX IF NOT EXISTS idx_customer_email ON "Customer"("email");

-- Gallery Photo indexes
CREATE INDEX IF NOT EXISTS idx_gallery_photo_approved ON "GalleryPhoto"("approved", "createdAt" DESC);

-- Optimize query planner statistics
ANALYZE;
