# Carbon Footprint Tracking

Understanding environmental impact measurement in LocalGreenChain.

## Why Track Carbon?

Food production accounts for 26% of global greenhouse gas emissions. By tracking carbon at every step, LocalGreenChain enables:

1. **Awareness** - Know the impact of your food
2. **Optimization** - Choose lower-carbon options
3. **Reduction** - Make data-driven improvements
4. **Comparison** - See savings vs conventional

## Carbon Sources in Agriculture

### Traditional Supply Chain

```
┌─────────────────────────────────────────────────────────────────┐
│              CONVENTIONAL FOOD CARBON FOOTPRINT                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  Growing       Transport    Processing   Retail      Consumer   │
│  ████████      ████████████ ████         ████        ████       │
│   30%            50%         8%           7%          5%        │
│                                                                  │
│  Total: 2.5 kg CO2 per kg produce (average)                     │
│                                                                  │
│  Key Contributors:                                               │
│  - Fertilizer production and application                         │
│  - Long-distance trucking (refrigerated)                         │
│  - International shipping                                        │
│  - Cold storage facilities                                       │
│  - Last-mile delivery                                            │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘
```

### LocalGreenChain Model

```
┌─────────────────────────────────────────────────────────────────┐
│              LOCALGREENCHAIN CARBON FOOTPRINT                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  Growing       Transport    Processing   Direct                  │
│  ████          ██           █            (Consumer)              │
│   60%           25%          10%          5%                     │
│                                                                  │
│  Total: 0.3 kg CO2 per kg produce (average)                     │
│                                                                  │
│  Savings: 88% reduction vs conventional                          │
│                                                                  │
│  Why Lower:                                                      │
│  - Local production (short transport)                            │
│  - Organic/sustainable methods                                   │
│  - No cold storage needed (fresh delivery)                       │
│  - Efficient vertical farming                                    │
│  - Direct grower-consumer connection                             │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘
```

## Transport Carbon Factors

### By Transport Method

| Method | kg CO2 / km / kg | Notes |
|--------|------------------|-------|
| Walking | 0 | Zero emissions |
| Bicycle | 0 | Zero emissions |
| Electric Vehicle | 0.02 | Grid-dependent |
| Hybrid Vehicle | 0.08 | Partial electric |
| Gasoline Vehicle | 0.12 | Standard car |
| Diesel Truck | 0.15 | Delivery truck |
| Electric Truck | 0.03 | Large EV |
| Refrigerated Truck | 0.25 | Cooling adds load |
| Rail | 0.01 | Very efficient |
| Ship | 0.008 | Bulk efficiency |
| Air Freight | 0.50 | Highest impact |
| Drone | 0.01 | Short distance only |

### Calculation Formula

```typescript
function calculateTransportCarbon(
  method: TransportMethod,
  distanceKm: number,
  weightKg: number
): number {
  const factor = CARBON_FACTORS[method];
  return factor * distanceKm * weightKg;
}

// Example: 20 kg tomatoes, 25 km by electric vehicle
const carbon = 0.02 * 25 * 20; // = 10 kg CO2
```

## Food Miles

### Definition

Food miles = total distance food travels from origin to consumer.

### Why It Matters

```
California Tomato to NYC:
├── Farm to packing: 20 miles
├── Packing to distribution: 50 miles
├── Distribution to cross-country truck: 10 miles
├── California to NYC: 2,800 miles
├── NYC distribution to store: 30 miles
├── Store to consumer: 5 miles
└── TOTAL: 2,915 miles

Local Brooklyn Tomato:
├── Farm to consumer: 12 miles
└── TOTAL: 12 miles

Savings: 99.6% reduction in food miles
```

### Distance Calculation

LocalGreenChain uses the Haversine formula:

```typescript
function calculateDistance(
  from: { lat: number; lon: number },
  to: { lat: number; lon: number }
): number {
  const R = 6371; // Earth's radius in km

  const dLat = toRadians(to.lat - from.lat);
  const dLon = toRadians(to.lon - from.lon);

  const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(toRadians(from.lat)) *
            Math.cos(toRadians(to.lat)) *
            Math.sin(dLon/2) * Math.sin(dLon/2);

  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

  return R * c; // Distance in km
}
```

## Cumulative Tracking

### Per-Plant Journey

Every transport event adds to the total:

```typescript
interface PlantJourney {
  plantId: string;
  events: TransportEvent[];

  totalFoodMiles: number;     // Sum of all distances
  totalCarbonKg: number;      // Sum of all emissions

  // Breakdown
  milesPerStage: {
    seedAcquisition: number;
    growing: number;
    harvest: number;
    distribution: number;
  };
}
```

### Per-User Footprint

```typescript
interface EnvironmentalImpact {
  totalCarbonKg: number;
  totalFoodMiles: number;

  // Efficiency metrics
  carbonPerKgProduce: number;
  milesPerKgProduce: number;

  // Breakdown by transport method
  breakdownByMethod: {
    [method: string]: {
      distance: number;
      carbon: number;
    }
  };

  // Comparison to conventional
  comparisonToConventional: {
    carbonSaved: number;
    milesSaved: number;
    percentageReduction: number;
  };
}
```

## Conventional Comparison

### Baseline Assumptions

| Produce | Conventional (kg CO2/kg) | Local (kg CO2/kg) | Savings |
|---------|--------------------------|-------------------|---------|
| Tomatoes | 2.8 | 0.32 | 89% |
| Lettuce | 1.5 | 0.15 | 90% |
| Peppers | 2.2 | 0.28 | 87% |
| Basil | 1.8 | 0.18 | 90% |
| Strawberries | 2.0 | 0.25 | 88% |

### Calculation

```typescript
function compareToConventional(
  totalCarbonKg: number,
  totalWeightKg: number
): Comparison {
  // Conventional average: 2.5 kg CO2 per kg produce
  // Conventional miles: 1,500 average

  const conventionalCarbon = totalWeightKg * 2.5;
  const conventionalMiles = totalWeightKg * 1500;

  return {
    carbonSaved: Math.max(0, conventionalCarbon - totalCarbonKg),
    milesSaved: Math.max(0, conventionalMiles - totalFoodMiles),
    percentageReduction: Math.round(
      (1 - totalCarbonKg / conventionalCarbon) * 100
    )
  };
}
```

## Vertical Farming Impact

### Energy-Based Carbon

Vertical farms trade transport carbon for energy carbon:

```
Outdoor Growing:        0.2 kg CO2/kg (minimal energy)
+ Transport (1,500 mi): 2.3 kg CO2/kg
= Total:                2.5 kg CO2/kg

Vertical Farm:          0.25 kg CO2/kg (lighting/HVAC)
+ Transport (10 mi):    0.02 kg CO2/kg
= Total:                0.27 kg CO2/kg

Net Savings: 89%
```

### Factors Affecting VF Carbon

| Factor | Impact | Optimization |
|--------|--------|--------------|
| Grid carbon intensity | High | Renewable energy |
| LED efficiency | Medium | Latest technology |
| HVAC efficiency | Medium | Heat pumps |
| Insulation | Low | Building design |

## Reporting

### Environmental Impact Dashboard

```
┌─────────────────────────────────────────────────────────────────┐
│              YOUR ENVIRONMENTAL IMPACT                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  This Month                       vs Conventional                │
│  ────────────                     ─────────────────              │
│  Total Produce: 45 kg             You Saved:                     │
│  Carbon: 8.5 kg CO2               □ 104 kg CO2                   │
│  Food Miles: 245 km               □ 67,255 food miles            │
│                                   □ 93% reduction                │
│                                                                  │
│  Breakdown by Transport Method                                   │
│  ─────────────────────────────                                   │
│  Electric Vehicle: ████████████████ 180 km (0.8 kg CO2)         │
│  Walking:          █████ 45 km (0 kg CO2)                        │
│  Bicycle:          ██ 20 km (0 kg CO2)                           │
│                                                                  │
│  Your Ranking: Top 15% of consumers                              │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘
```

## Best Practices

### For Growers

1. **Local seed sources** - Reduce acquisition miles
2. **Clean transport** - Electric, bicycle, walk
3. **Batch deliveries** - Combine shipments
4. **Direct sales** - Skip distribution chain
5. **Renewable energy** - Solar for operations

### For Consumers

1. **Buy local** - Shorter supply chain
2. **Accept imperfect** - Reduces waste transport
3. **Plan purchases** - Fewer delivery trips
4. **Pick up when possible** - Zero delivery carbon
5. **Choose in-season** - No climate-controlled transport

### For System Operators

1. **Route optimization** - Minimize total distance
2. **Load optimization** - Full trucks, no empty returns
3. **Hub placement** - Strategic distribution points
4. **Electric fleet** - Transition to zero-emission
5. **Carbon tracking** - Continuous monitoring

## Future Improvements

- **Scope 3 emissions** - Full lifecycle analysis
- **Carbon offsetting** - Tree planting, etc.
- **Carbon credits** - Tradeable savings
- **Real-time tracking** - GPS + carbon calculation
- **AI optimization** - Minimize total footprint
