""" @router StorefrontRouter (Backend/app/api/v1/routers/StorefrontRouter.py) @purpose Read-only public endpoints for footer info, store settings, mega menu, catalog filters, trust badges, blog posts, and product reviews — all backed by StorefrontCmsService. """ from fastapi import APIRouter, Depends, Query, Response from sqlalchemy.orm import Session from typing import Any, Dict, List, Optional from app.core.database.db_session import get_db from app.services.StorefrontCmsService import StorefrontCmsService from app.schemas.StorefrontCmsSchema import ( StorefrontReviewCreate, ProductReviewSummaryResponse, ) router = APIRouter(prefix="/api/v1/storefront", tags=["Storefront Dynamic Services"]) @router.get("/footer-info") def get_footer_info(response: Response, db: Session = Depends(get_db)) -> Any: """Returns dynamic footer contact info, social links, navigation columns, and payment icons.""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" svc = StorefrontCmsService(db) return svc.get_footer_info() @router.get("/settings") def get_storefront_settings(response: Response, db: Session = Depends(get_db)) -> Any: """Returns store branding settings (logo, wordmark, support phone, advance_percent, etc.).""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" svc = StorefrontCmsService(db) return svc.get_settings() @router.get("/mega-menu") def get_mega_menu( response: Response, nav_key: Optional[str] = Query(None, pattern="^(shop|deals|products)$"), db: Session = Depends(get_db), ) -> Any: """ Returns mega-menu configuration. If nav_key is supplied returns single menu config. If omitted returns all three (shop, deals, products) in one payload. """ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" svc = StorefrontCmsService(db) return svc.get_mega_menu(nav_key) @router.get("/catalog-filters") def get_catalog_filters(response: Response, db: Session = Depends(get_db)) -> Any: """Returns highlight filter tabs and price range buckets for the shop catalog page.""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" svc = StorefrontCmsService(db) return svc.get_catalog_filters() @router.get("/reviews/{product_id}", response_model=ProductReviewSummaryResponse) def get_product_reviews( product_id: str, page: int = Query(1, ge=1), limit: int = Query(10, ge=1, le=50), sort: str = Query("newest"), db: Session = Depends(get_db) ) -> Any: """Returns paginated approved customer reviews and star distribution for a given product.""" from app.services.StorefrontService import StorefrontService svc = StorefrontService(db) return svc.get_reviews(product_id=product_id, page=page, limit=limit, sort=sort) @router.post("/reviews/{product_id}", status_code=201) def submit_product_review( product_id: str, payload: StorefrontReviewCreate, db: Session = Depends(get_db) ) -> Any: """Submits a customer review with server-side verified purchase validation.""" from app.services.StorefrontService import StorefrontService svc = StorefrontService(db) return svc.submit_review(product_id=product_id, payload=payload) @router.get("/trust-badges") def get_trust_badges(db: Session = Depends(get_db)) -> List[Dict[str, Any]]: """Returns dynamic storefront trust badges.""" from app.services.DeliveryService import DeliveryService delivery_settings = DeliveryService.get_delivery_settings(db) if delivery_settings["free_delivery_enabled"]: min_amt = int(delivery_settings["free_delivery_min_order_amount"]) shipping_title = "Free Shipping" shipping_sub = f"On order over ₹{min_amt:,}" else: shipping_title = "Express Delivery" shipping_sub = "Fast & Insured Pan-India" return [ {"id": "tb1", "title": shipping_title, "subtitle": shipping_sub, "icon": "truck"}, {"id": "tb2", "title": "Flexible & Easy Return", "subtitle": "Return within 14 days", "icon": "refresh"}, {"id": "tb3", "title": "24/7 Support Services", "subtitle": "Any Time Customer Support", "icon": "headset"}, {"id": "tb4", "title": "Secure payment", "subtitle": "100% Fast & Secure Payment", "icon": "shield"}, ] @router.get("/blog-posts") def get_blog_posts() -> List[Dict[str, Any]]: """Returns dynamic tech blog articles.""" return [ {"post_id": "bp1", "title": "How to Write a Blog Post Your Readers Will Love in 5 Steps", "excerpt": "Why the world would end without travel coupons. The 16 worst...", "author": "Admin", "date": "July 24, 2026", "image_url": ""}, {"post_id": "bp2", "title": "9 Content Marketing Trends and Ideas to Increase Traffic", "excerpt": "Why do people think wholesale accessories are a good idea? Unbelievable...", "author": "Technician", "date": "July 22, 2026", "image_url": ""}, {"post_id": "bp3", "title": "The Ultimate Guide to Marketing Strategies to Improve Sales", "excerpt": "Many things about electronic devices your kids don't want you to...", "author": "Store Manager", "date": "July 19, 2026", "image_url": ""}, {"post_id": "bp4", "title": "50 Best Sales Questions to Determine Your Customer's Needs", "excerpt": "The unconventional guide to the software applications...", "author": "Support Desk", "date": "July 15, 2026", "image_url": ""}, ] @router.get("/banners") def get_storefront_banners(response: Response, db: Session = Depends(get_db)) -> List[Dict[str, Any]]: """Returns hero banners for the storefront homepage (BUG-04 fix).""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" svc = StorefrontCmsService(db) banners = svc.get_content_by_region(page="home", region="hero", content_type="hero_banner") if banners: return [ { "banner_id": b.content_id, "title": b.title, "subtitle": b.subtitle or "", "image_url": b.image_url or "", "button_text": b.button_text or "Shop Now", "button_url": b.button_url or "/shop" } for b in banners ] return [ { "banner_id": "b1", "title": "Smartphone Repair & Spare Parts", "subtitle": "100% Genuine OEM Displays, Batteries & Accessories", "image_url": "/assets/hero-banner-1.webp", "button_text": "Explore Catalog", "button_url": "/shop" }, { "banner_id": "b2", "title": "Fast Doorstep Repair Service", "subtitle": "Certified Engineers & 6-Month iFixKart Warranty", "image_url": "/assets/hero-banner-2.webp", "button_text": "Book Repair", "button_url": "/services" } ]