# Backend - Astrology Birthdate Chart

**Project Name:** Astrology Birthdate Chart

This directory contains the FastAPI backend for the astrology project. It provides astrology calculations, chart generation, and API endpoints used by the frontend.

## 📁 Main Backend Files

- `main.py` - FastAPI application, router assembly, CORS setup, static chart file mounting, and service registration.
- `astrology.py` - Planet position calculations using Swiss Ephemeris.
- `birthday_chart.py` - Birth chart generation logic.
- `astrology_chart.py` - Chart rendering utilities.
- `chart_patterns.py` - Pattern detection and astrology pattern helpers.
- `chart_storage.py` - Database router for storing and retrieving generated charts.
- `chart_utils.py` - Shared utility functions for chart operations.
- `models.py` - SQLAlchemy models for chart persistence.
- `database.py` - Database engine and session setup.
- `schemas.py` - Pydantic request/response schemas.
- `synastry-chart.py` - Custom synastry chart generator loaded dynamically in `main.py`.
- `lunar_return_calculation.py` - Lunar return chart calculations.
- `solar_return_calculation.py` - Solar return chart calculations.
- `progressed_chart.py` - Progressed chart endpoint router.
- `saturn_return_chart.py` - Saturn return chart endpoint router.
- `transit_chart_generate.py` - Transit chart generation logic.
- `upcoming_aspects.py` - Upcoming aspects calculator.
- `horoscope.py` - Horoscope endpoint router.
- `admin_auth.py` - Admin authentication router.
- `ai_horoscope.py` - AI horoscope helper.
- `services/planet_service.py` - Planet service utilities.

## 📦 Supporting Directories

- `charts/` - Static chart output directories mounted at `/charts`.
- `ephe/` - Ephemeris data files used for astrology calculations.

## ⚙️ Environment

The backend expects a `.env` file in the `backend/` folder containing at least:

```env
FRONTEND_URL=http://localhost:5173
```

This value is required by `main.py` for CORS configuration.

## 🚀 Run Locally

1. Activate the Python virtual environment from the project root:

```bash
source ../venv/bin/activate
```

2. Install dependencies if not already installed:

```bash
pip install fastapi uvicorn python-dotenv pydantic  pyswisseph
```

3. Start the backend:

```bash
cd backend
python main.py
```

Or run with Uvicorn manually:

```bash
cd backend
uvicorn main:app --reload --host 0.0.0.0 --port 8000
```

## 🌐 API Overview

The backend exposes several endpoints for astrology chart generation and analysis, including:

- `/` - API health/home
- `/synastry-chart` - Generate synastry chart SVG
- `/calculators/birth-chart` - Generate a birth chart
- `/calculator/moon-phase` - Calculate moon phase details
- `/calculators/lunar-return` - Create lunar return result
- `/calculators/solar-return` - Create solar return result
- `/calculators/saturn-return-chart` - Saturn return result
- `/chart-data` - Planet/chart data
- `/synastry-aspects` - Synastry aspects
- `/synastry-patterns` - Synastry patterns

> See `APIS.txt` for the full list of backend API routes.

## 📌 Notes

- Static chart files are served from `backend/charts` under the `/charts` route.
- The backend is configured to allow origins from the frontend and local Vite dev URLs.
- `synastry-chart.py` uses a hyphenated filename and is loaded dynamically with `importlib`.
