# Ecoloop Backend

Ecoloop is a Django + DRF backend for recycling businesses and factories to manage pickups, pricing, invoices/payouts, and a bidding marketplace for scrap lots. It supports multi-organization setups, JWT auth, and a Tailwind-powered UI layer.

## Quick Start
- Requirements: Python 3.11, Postgres, Node (for Tailwind in dev)
- Environment: copy `.env` and set `SECRET_KEY`, `DEBUG`, `DB_*`, `ALLOWED_HOSTS`
- Run (Docker): `docker-compose up --build` (web on http://localhost:8024)
- Run (local):
  - `python manage.py migrate`
  - `python manage.py runserver`
  - Optional demo: `python manage.py seed_ecoloop`
- API docs: Swagger UI at `/api/schema/swagger-ui/`

## Project Scopes (Summary)
- Core operations: customers, sites, service agreements, materials, units; pickup lifecycle (request → schedule/assign → collect → weigh ticket → complete).
- Pricing & finance: org/customer price lists (buy/sell), invoice generation from weigh tickets, payments, and payouts for buy-priced materials.
- Bidding marketplace: factories post scrap listings (open/invite-only), recyclers bid with min-increment/reserve; award creates a pickup order.
- Users & portal: roles (owner, manager, driver, customer, auditor); JWT auth; customer/driver-facing endpoints for assignments and status.
- Compliance & docs: upload/attach documents (proofs, photos, certificates); audit logs for key state changes.
- Reporting: benefit leaderboards and volume/revenue summaries (period filters).
- Multi-org & locale: organization scoping, currency codes (THB/USD), units (kg/lb); env-driven config.

## Key Commands
- `python manage.py migrate` — apply DB migrations
- `python manage.py seed_ecoloop` — seed demo org, users, materials, pickup, weigh ticket, invoice, listing/bid
- `python manage.py ecoloop_auto_close_award` — auto-close/award expired listings

## Multisite (Local Testing)
- Purpose: Serve multiple sites on one codebase using Django Sites.
- Config:
  - Enabled in `ecoloop/settings.py` via `django.contrib.sites` and `SITE_ID`.
  - `.env` includes defaults for local hosts: `ALLOWED_HOSTS=localhost,127.0.0.1,.lvh.me`
  - `.env` CSRF for Docker port: `CSRF_TRUSTED_ORIGINS=http://localhost:8024,http://*.lvh.me:8024`
- Run:
  - `python manage.py migrate` (applies Sites tables)
  - In Admin → Sites, add domains like `alpha.lvh.me` and `beta.lvh.me`.
  - Visit `http://alpha.lvh.me:8024` and `http://beta.lvh.me:8024`.
  - Attach site-specific content in apps (e.g., CMS) by filtering on `get_current_site(request)` or adding a `ForeignKey` to `Site`.

## Structure
- `ecoloop/` settings and URLs; `api/` public endpoints; `recycle_core/` domain models/APIs
- Assets: `templates/`, `static/`, `media/`; Entrypoints: `manage.py`, `docker-compose.yml`, `Dockerfile`
