# Feature Spec: Environmental Monitoring & Financial Tracking

**Priority**: 🟡 Medium  
**Phase**: 13  
**Status**: ✅ Implemented

---

## Overview

Advanced features for environmental sensor monitoring, financial tracking with cost-per-gram analysis, and AI-powered yield predictions.

---

## Environmental Monitoring

### Models

#### Sensor

- `id`: UUID
- `name`: string
- `type`: enum (TEMPERATURE, HUMIDITY, CO2, LIGHT_PAR, PH, EC, VPD, SOIL_MOISTURE)
- `roomId`: FK (optional)
- `location`: string (e.g., "North wall")
- `deviceId`: string (hardware ID)
- `minThreshold`: number
- `maxThreshold`: number
- `isActive`: boolean

#### SensorReading

- `id`: UUID
- `sensorId`: FK
- `value`: number
- `unit`: string (°F, %, ppm, etc.)
- `timestamp`: datetime

#### EnvironmentAlert

- `id`: UUID
- `sensorId`: FK (optional)
- `roomId`: FK (optional)
- `type`: string (TEMP_HIGH, HUMIDITY_LOW, etc.)
- `severity`: enum (INFO, WARNING, CRITICAL, EMERGENCY)
- `message`: string
- `acknowledgedAt`: datetime
- `resolvedAt`: datetime

### API Endpoints

- `GET /api/environment/sensors` - List sensors
- `POST /api/environment/sensors` - Create sensor
- `POST /api/environment/sensors/:id/readings` - Submit reading
- `GET /api/environment/sensors/:id/readings` - Get reading history
- `GET /api/environment/alerts` - Get alerts
- `POST /api/environment/alerts/:id/acknowledge` - Ack alert
- `GET /api/environment/dashboard` - Real-time dashboard

---

## Financial Tracking

### Models

#### FinancialTransaction

- `id`: UUID
- `type`: enum (EXPENSE, REVENUE, ADJUSTMENT)
- `category`: enum (LABOR, NUTRIENTS, SUPPLIES, EQUIPMENT, UTILITIES, etc.)
- `amount`: number
- `description`: string
- `batchId`: FK (optional)
- `date`: datetime

#### BatchCost

- `id`: UUID
- `batchId`: FK (unique)
- `laborCost`: number
- `nutrientCost`: number
- `utilityCost`: number
- `totalCost`: number
- `yieldGrams`: number
- `costPerGram`: number

#### Sale

- `id`: UUID
- `batchId`: FK (optional)
- `product`: string
- `quantity`: number
- `pricePerUnit`: number
- `totalPrice`: number

### API Endpoints

- `GET /api/financial/transactions` - List transactions with totals
- `POST /api/financial/transactions` - Create transaction
- `GET /api/financial/sales` - List sales
- `POST /api/financial/sales` - Record sale
- `GET /api/financial/batches/:id/costs` - Batch cost breakdown
- `GET /api/financial/reports/profit-loss` - P&L report
- `GET /api/financial/reports/category-breakdown` - Expense breakdown
- `GET /api/financial/reports/cost-per-gram` - CPG analysis

---

## AI/ML Insights

### Models

#### YieldPrediction

- `id`: UUID
- `batchId`: FK
- `predictedYield`: number (grams)
- `confidence`: number (0-1)
- `factors`: JSON
- `actualYield`: number (filled after harvest)
- `accuracy`: number

#### AnomalyDetection

- `id`: UUID
- `entityType`: string
- `entityId`: FK
- `anomalyType`: string
- `severity`: string
- `description`: string
- `isResolved`: boolean

### API Endpoints

- `POST /api/insights/predict/yield` - Generate yield prediction
- `GET /api/insights/predictions/:batchId` - Get predictions history
- `POST /api/insights/detect/anomalies` - Run anomaly detection
- `GET /api/insights/anomalies` - List anomalies
- `GET /api/insights/dashboard` - AI insights overview

---

## Future Enhancements

- Real ML model integration (TensorFlow/PyTorch)
- Predictive maintenance
- Automated threshold adjustment
- Integration with accounting software
