# Ads Manager Service Architecture

## Tổng Quan / Overview

```mermaid
graph TB
    subgraph "ads-manager-service"
        API[API Layer]
        APP[Application Layer]
        DOM[Domain Layer]
        INF[Infrastructure Layer]
    end
    
    API --> APP
    APP --> DOM
    APP --> INF
    INF --> DOM
    
    subgraph "External"
        IAM[IAM Service]
        STOR[Storage Service]
        BILL[ads-billing]
        SERVE[ads-serving]
        MQ[RabbitMQ]
    end
    
    API --> IAM
    API --> STOR
    INF --> MQ
    MQ --> BILL
    MQ --> SERVE
```

## Domain Aggregates

### Campaign Aggregate
- **Campaign** (Root): Mục tiêu, trạng thái, ngân sách tổng
- **CampaignObjective**: Awareness, Traffic, Conversion
- **CampaignBudget**: Daily/Lifetime, Amount

### AdSet Aggregate
- **AdSet** (Root): Nhóm quảng cáo
- **Targeting**: Core/Interest/Custom/Lookalike
- **Placement**: Feed, Story, Search
- **BidStrategy**: CPC, CPM, OCPM

### Ad Aggregate
- **Ad** (Root): Mẫu quảng cáo
- **AdCreative**: Media, Headline, CTA
- **AdReviewStatus**: Pending/Approved/Rejected

### Audience Aggregate
- **CustomAudience**: Uploaded lists
- **LookalikeAudience**: Similar users
- **SavedAudience**: Reusable templates

## Data Flow

```
Create Campaign → Validate → Save → Publish Event → ads-serving syncs
```

## Database Schema

```sql
-- Campaigns
CREATE TABLE campaigns (
    id UUID PRIMARY KEY,
    advertiser_id UUID NOT NULL,
    name VARCHAR(255) NOT NULL,
    objective VARCHAR(50) NOT NULL,
    status VARCHAR(50) NOT NULL,
    budget_type VARCHAR(20),
    budget_amount DECIMAL(18,2),
    created_at TIMESTAMP DEFAULT NOW()
);

-- AdSets
CREATE TABLE ad_sets (
    id UUID PRIMARY KEY,
    campaign_id UUID REFERENCES campaigns(id),
    name VARCHAR(255),
    status VARCHAR(50),
    targeting JSONB,
    bid_strategy VARCHAR(50),
    bid_amount DECIMAL(18,2)
);

-- Ads
CREATE TABLE ads (
    id UUID PRIMARY KEY,
    ad_set_id UUID REFERENCES ad_sets(id),
    name VARCHAR(255),
    creative_url TEXT,
    headline VARCHAR(255),
    cta VARCHAR(50),
    review_status VARCHAR(50)
);
```
