# 02 - Project & Directory Structure

Is document me hum samjhenge ki **M1.1 implementation me kaun-kaun se files aur folders bane hain** aur har file/folder ka system me kya role hai.

---

## 1. Directory Tree (M1.1 Scope)

Here is the exact folder structure present in `backend/`:

```text
backend/
├── .env                  <-- Local secret configuration (URI, DB Name)
├── .env.example          <-- Sample template for configuration
├── requirements.txt      <-- Python third-party dependencies list
├── app/
│   ├── __init__.py       <-- Marks 'app' as a Python package
│   ├── main.py           <-- FastAPI App Entry point & lifespan
│   ├── config.py         <-- Settings loader using Pydantic BaseSettings
│   ├── database.py       <-- MongoDB Async connection management (AsyncMongoClient)
│   ├── models/
│   │   ├── __init__.py
│   │   └── tenant.py     <-- Pydantic schemas (TenantCreate, TenantResponse)
│   ├── repositories/
│   │   ├── __init__.py
│   │   └── tenant_repository.py  <-- Database operations (insert, find)
│   └── routers/
│       ├── __init__.py
│       └── tenant.py     <-- API endpoint handler (POST /api/v1/tenant/register)
└── tests/
    ├── __init__.py
    ├── conftest.py       <-- Pytest test configuration & TestClient fixture
    └── test_tenant.py    <-- Automated integration tests for tenant API
```

---

## 2. Why Each File Exists (Role & Responsibility)

### Root Level Files

#### `requirements.txt`
- **Role:** Python package list.
- **Why it exists:** Python application ko run karne ke liye jo libraries chahiye (FastAPI, PyMongo, Pydantic, Certifi, Pytest), unhe standardized format me list karta hai taaki direct `pip install -r requirements.txt` chalaya ja sake.

#### `.env` and `.env.example`
- **Role:** Secrets and environment configuration.
- **Why it exists:** Passwords, connection strings (`mongodb://...`), ya database names ko code me hardcode karna security risk hai. `.env` me real values rahti hain, aur `.env.example` sample values provide karta hai without sensitive credentials.

---

### Application Core (`app/`)

#### `app/main.py`
- **Role:** Main application starting point.
- **Why it exists:** Yahan FastAPI ka instance create hota hai (`app = FastAPI(...)`). Ye saare routers ko connect karta hai aur application shutdown hone par database connections clean karwata hai.

#### `app/config.py`
- **Role:** Centralized configuration management.
- **Why it exists:** Reads `.env` file automatically and exposes environment variables as Python attributes (e.g., `settings.mongodb_uri`).

#### `app/database.py`
- **Role:** MongoDB Connection Manager.
- **Why it exists:** Manages `AsyncMongoClient` lifecycle. It connects to MongoDB Atlas/Local DB safely with TLS certificates (`certifi`) and provides `get_database()` helper to repositories.

---

### Data Layers inside `app/`

#### `app/models/tenant.py`
- **Role:** Data Contracts & Schema Validation.
- **Why it exists:** Defines what data the API accepts from users (`TenantCreate`) and what data it sends back (`TenantResponse`). Also cleans up incoming input (e.g. converting `HTTP://MyDomain.COM` -> `mydomain.com`).

#### `app/repositories/tenant_repository.py`
- **Role:** Database Queries (Data Access Layer).
- **Why it exists:** Routes (API code) ko MongoDB ke complex queries se alag rakhta hai. agar kal ko MongoDB ki jagah PostgreSQL use karna pade, toh sirf repository change hogi, router code same rahega.

#### `app/routers/tenant.py`
- **Role:** HTTP Route Handler / Controller.
- **Why it exists:** Receives incoming HTTP requests on `/api/v1/tenant/register`, checks HTTP rules (like checking duplicate domain status code `400`), calls the repository, and returns HTTP `201 Created`.

---

### Test Suite (`tests/`)

#### `tests/conftest.py`
- **Role:** Test environment setup.
- **Why it exists:** Re-routes tests to a separate test database (`chatbot_test_db`) so production/development data does not get contaminated during test runs. Sets up `TestClient` fixture.

#### `tests/test_tenant.py`
- **Role:** Automated Unit/Integration tests.
- **Why it exists:** Automatically verifies that tenant registration works correctly, rejects duplicate domains, and handles invalid inputs properly.
