from datetime import datetime from typing import Any from pydantic import BaseModel, EmailStr, Field, field_validator from app.schemas.common import InputModel, OrmModel, Phone from app.schemas.enquiry import EnquiryListItem from app.services.image_processing import media_url # ----------------------------------------------------------------- services class ServicePublic(BaseModel): id: int title: str slug: str short_description: str | None description: str | None icon: str | None sort_order: int image_url: str | None thumbnail_url: str | None @staticmethod def _fields(s) -> dict: return { "id": s.id, "title": s.title, "slug": s.slug, "short_description": s.short_description, "description": s.description, "icon": s.icon, "sort_order": s.sort_order, "image_url": media_url(s.image_watermarked_path), "thumbnail_url": media_url(s.image_thumbnail_path), } @classmethod def build(cls, s) -> "ServicePublic": return cls(**cls._fields(s)) class ServiceAdmin(ServicePublic): is_published: bool created_at: datetime updated_at: datetime @classmethod def build(cls, s) -> "ServiceAdmin": return cls( **cls._fields(s), is_published=s.is_published, created_at=s.created_at, updated_at=s.updated_at, ) class ServiceCreate(InputModel): title: str = Field(min_length=2, max_length=150) slug: str | None = Field(default=None, max_length=150, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$") short_description: str | None = Field(default=None, max_length=300) description: str | None = Field(default=None, max_length=10_000) icon: str | None = Field(default=None, max_length=60) sort_order: int = Field(default=0, ge=0, le=100_000) is_published: bool = False class ServiceUpdate(InputModel): title: str | None = Field(default=None, min_length=2, max_length=150) slug: str | None = Field(default=None, max_length=150, pattern=r"^[a-z0-9]+(?:-[a-z0-9]+)*$") short_description: str | None = Field(default=None, max_length=300) description: str | None = Field(default=None, max_length=10_000) icon: str | None = Field(default=None, max_length=60) sort_order: int | None = Field(default=None, ge=0, le=100_000) is_published: bool | None = None # ----------------------------------------------------------------- testimonials def _check_video_url(v: str | None) -> str | None: if v is None or not v.strip(): return None v = v.strip() if not v.lower().startswith(("https://", "http://")): raise ValueError("Video link must start with https://") return v class TestimonialPublic(BaseModel): id: int customer_name: str designation: str | None location: str | None content: str | None video_url: str | None rating: int project_id: int | None sort_order: int image_url: str | None thumbnail_url: str | None @staticmethod def _fields(t) -> dict: return { "id": t.id, "customer_name": t.customer_name, "designation": t.designation, "location": t.location, "content": t.content, "video_url": t.video_url, "rating": t.rating, "project_id": t.project_id, "sort_order": t.sort_order, "image_url": media_url(t.image_watermarked_path), "thumbnail_url": media_url(t.image_thumbnail_path), } @classmethod def build(cls, t) -> "TestimonialPublic": return cls(**cls._fields(t)) class TestimonialAdmin(TestimonialPublic): is_published: bool created_at: datetime updated_at: datetime @classmethod def build(cls, t) -> "TestimonialAdmin": return cls(**cls._fields(t), is_published=t.is_published, created_at=t.created_at, updated_at=t.updated_at) class TestimonialCreate(InputModel): customer_name: str = Field(min_length=2, max_length=150) designation: str | None = Field(default=None, max_length=150) location: str | None = Field(default=None, max_length=150) content: str | None = Field(default=None, max_length=3000) video_url: str | None = Field(default=None, max_length=500) rating: int = Field(default=5, ge=1, le=5) project_id: int | None = None sort_order: int = Field(default=0, ge=0, le=100_000) is_published: bool = True @field_validator("video_url") @classmethod def _video(cls, v: str | None) -> str | None: return _check_video_url(v) class TestimonialUpdate(InputModel): customer_name: str | None = Field(default=None, min_length=2, max_length=150) designation: str | None = Field(default=None, max_length=150) location: str | None = Field(default=None, max_length=150) content: str | None = Field(default=None, max_length=3000) video_url: str | None = Field(default=None, max_length=500) rating: int | None = Field(default=None, ge=1, le=5) project_id: int | None = None sort_order: int | None = Field(default=None, ge=0, le=100_000) is_published: bool | None = None @field_validator("video_url") @classmethod def _video(cls, v: str | None) -> str | None: return _check_video_url(v) # ----------------------------------------------------------------- contact messages class ContactCreate(InputModel): name: str = Field(min_length=2, max_length=150) email: EmailStr phone: Phone | None = None subject: str | None = Field(default=None, max_length=200) message: str = Field(min_length=5, max_length=5000) website: str | None = Field(default=None, max_length=200) # honeypot @field_validator("phone", "subject", "website", mode="before") @classmethod def _blank_to_none(cls, v): return None if isinstance(v, str) and not v.strip() else v class ContactOut(OrmModel): id: int name: str email: str phone: str | None subject: str | None message: str is_read: bool created_at: datetime class ContactUpdate(InputModel): is_read: bool # ----------------------------------------------------------------- site content class SiteContentOut(OrmModel): key: str value: dict[str, Any] updated_at: datetime class SiteContentUpsert(InputModel): value: dict[str, Any] = Field(description="Arbitrary JSON object for this content block") # ----------------------------------------------------------------- dashboard class DashboardStats(BaseModel): total_enquiries: int new_enquiries: int contacted: int site_visits_scheduled: int quotations_sent: int converted: int rejected: int enquiries_last_30_days: int completed_projects: int published_projects: int total_projects: int published_services: int unread_contact_messages: int recent_enquiries: list[EnquiryListItem]