File size: 2,960 Bytes
5e5973c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from fastapi import APIRouter, HTTPException, Depends
from firebase_admin import firestore
from pydantic import BaseModel, Field
from typing import List
import time

router = APIRouter(prefix="/v1/community", tags=["Community"])


# ---------- DB Helper (lazy init, safe) ----------
def get_db():
    try:
        return firestore.client()
    except Exception:
        raise HTTPException(status_code=500, detail="Database not initialized")


# ---------- Schemas (important for API selling & docs) ----------
class BrandBriefCreate(BaseModel):
    brand_id: str = Field(..., min_length=1)
    budget: int = Field(..., gt=0)
    requirements: str = Field(..., min_length=5)


class ReviewCreate(BaseModel):
    from_id: str
    to_id: str
    rating: int = Field(..., ge=1, le=5)
    comment: str = Field(..., min_length=3)


# ---------- 1. GET ALL BRIEFS (Marketplace feed) ----------
@router.get("/brand/briefs")
async def get_all_briefs():
    db = get_db()
    try:
        docs = (
            db.collection("community_board")
            .order_by("timestamp", direction=firestore.Query.DESCENDING)
            .limit(50)
            .stream()
        )

        briefs = []
        for doc in docs:
            data = doc.to_dict()
            data["id"] = doc.id
            briefs.append(data)

        return {
            "status": "success",
            "count": len(briefs),
            "data": briefs,
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ---------- 2. BRAND POSTING (Paid action) ----------
@router.post("/brand/post-brief")
async def post_brief(payload: BrandBriefCreate):
    db = get_db()

    FIXED_FEE_USD = 2.00  # 🔒 business rule (easy to change later)

    brief_data = {
        "brand_id": payload.brand_id,
        "budget": payload.budget,
        "requirements": payload.requirements,
        "fee_paid": FIXED_FEE_USD,
        "status": "active",
        "timestamp": firestore.SERVER_TIMESTAMP,
    }

    try:
        doc_ref = db.collection("community_board").add(brief_data)

        return {
            "status": "success",
            "message": "Brief posted successfully. Processing fee applied.",
            "doc_id": doc_ref[1].id,
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ---------- 3. FEEDBACK & REVIEW SYSTEM ----------
@router.post("/review")
async def leave_review(payload: ReviewCreate):
    db = get_db()

    review_data = {
        "reviewer": payload.from_id,
        "target": payload.to_id,
        "rating": payload.rating,
        "comment": payload.comment,
        "timestamp": firestore.SERVER_TIMESTAMP,
    }

    try:
        db.collection("reviews").add(review_data)
        return {
            "status": "success",
            "message": "Review added successfully",
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))