# Example: Seed to Harvest Workflow

Complete working example of tracking a plant from seed acquisition through harvest.

## Scenario

You're a grower who just acquired tomato seeds and wants to track them through the full growing cycle.

## Step 1: Acquire Seeds

### Record Seed Acquisition

```typescript
// POST /api/transport/seed-acquisition

const seedAcquisitionEvent = {
  // Event identity
  id: crypto.randomUUID(),
  timestamp: new Date().toISOString(),
  eventType: 'seed_acquisition',

  // Seed details
  seedBatchId: 'seeds-tomato-2024-001',
  sourceType: 'purchase',
  species: 'Solanum lycopersicum',
  variety: 'Cherokee Purple',
  quantity: 100,
  quantityUnit: 'seeds',
  generation: 1,
  certifications: ['organic', 'heirloom'],

  // Location tracking
  fromLocation: {
    latitude: 38.9072,
    longitude: -77.0369,
    locationType: 'seed_bank',
    facilityName: 'Heritage Seed Company',
    city: 'Washington',
    country: 'USA'
  },
  toLocation: {
    latitude: 40.7128,
    longitude: -74.0060,
    locationType: 'farm',
    facilityName: 'Brooklyn Urban Farm',
    city: 'Brooklyn',
    country: 'USA'
  },

  // Transport details
  transportMethod: 'local_delivery',
  distanceKm: 350,
  durationMinutes: 1440,  // 1 day shipping

  // Parties
  senderId: 'heritage-seeds-001',
  receiverId: 'grower-brooklyn-001',
  status: 'delivered'
};

// API call
const response = await fetch('/api/transport/seed-acquisition', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(seedAcquisitionEvent)
});

const { data: block } = await response.json();
console.log('Seed acquisition recorded:', block.hash);
// Output: Seed acquisition recorded: 000abc123...
```

## Step 2: Plant Seeds

### Record Planting Event

```typescript
// Wait until planting day...

// POST /api/transport/planting

const plantingEvent = {
  id: crypto.randomUUID(),
  timestamp: new Date().toISOString(),
  eventType: 'planting',

  // Link to seeds
  seedBatchId: 'seeds-tomato-2024-001',

  // New plants created
  plantIds: Array.from({ length: 25 }, (_, i) =>
    `plant-tomato-2024-${String(i + 1).padStart(3, '0')}`
  ),
  quantityPlanted: 25,

  // Planting details
  plantingMethod: 'indoor_start',
  growingEnvironment: 'greenhouse',
  sowingDepth: 6,  // mm
  spacing: 5,  // cm (seed starting trays)

  // Timeline
  expectedHarvestDate: new Date(
    Date.now() + 80 * 24 * 60 * 60 * 1000  // 80 days
  ).toISOString(),

  // Location (same farm, different area)
  fromLocation: {
    latitude: 40.7128,
    longitude: -74.0060,
    locationType: 'farm',
    facilityName: 'Brooklyn Urban Farm - Seed Storage'
  },
  toLocation: {
    latitude: 40.7128,
    longitude: -74.0060,
    locationType: 'greenhouse',
    facilityName: 'Brooklyn Urban Farm - Greenhouse'
  },

  transportMethod: 'walking',
  distanceKm: 0.05,
  durationMinutes: 5,

  senderId: 'grower-brooklyn-001',
  receiverId: 'grower-brooklyn-001',
  status: 'verified'
};

const response = await fetch('/api/transport/planting', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(plantingEvent)
});

const { data: block } = await response.json();
console.log('Planting recorded:', block.hash);
console.log('Plants created:', plantingEvent.plantIds.length);
// Output: 25 plants now tracked on blockchain
```

## Step 3: Track Growing Transport (Optional)

### Record Transplanting

```typescript
// 3 weeks later - transplant to final positions

// POST /api/transport/growing

const transplantEvent = {
  id: crypto.randomUUID(),
  timestamp: new Date().toISOString(),
  eventType: 'growing_transport',

  // Which plants
  plantIds: plantingEvent.plantIds,

  // Transplant details
  reason: 'transplant',
  plantStage: 'seedling',
  handlingMethod: 'potted',
  rootDisturbance: 'minimal',
  acclimatizationRequired: true,
  acclimatizationDays: 3,

  // Location (greenhouse to outdoor beds)
  fromLocation: {
    latitude: 40.7128,
    longitude: -74.0060,
    locationType: 'greenhouse',
    facilityName: 'Brooklyn Urban Farm - Greenhouse'
  },
  toLocation: {
    latitude: 40.7125,
    longitude: -74.0055,
    locationType: 'farm',
    facilityName: 'Brooklyn Urban Farm - Plot A'
  },

  transportMethod: 'walking',
  distanceKm: 0.1,
  durationMinutes: 30,

  // Environmental conditions during move
  environmentalConditions: {
    temperatureMin: 18,
    temperatureMax: 24,
    temperatureAvg: 21,
    humidityMin: 50,
    humidityMax: 70,
    humidityAvg: 60,
    lightExposure: 'ambient'
  },

  senderId: 'grower-brooklyn-001',
  receiverId: 'grower-brooklyn-001',
  status: 'verified'
};

await fetch('/api/transport/growing', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(transplantEvent)
});
```

## Step 4: Harvest

### Record Harvest Event

```typescript
// 80 days from planting - harvest time!

// POST /api/transport/harvest

const harvestEvent = {
  id: crypto.randomUUID(),
  timestamp: new Date().toISOString(),
  eventType: 'harvest',

  // Which plants
  plantIds: plantingEvent.plantIds,
  harvestBatchId: 'harvest-tomato-2024-001',

  // Harvest details
  harvestType: 'partial',  // More to come
  produceType: 'tomatoes',

  // Quantities
  grossWeight: 45,  // kg
  netWeight: 42,    // after culls
  weightUnit: 'kg',
  itemCount: 180,   // individual tomatoes

  // Quality
  qualityGrade: 'A',
  qualityNotes: 'Excellent color, firm texture, no blemishes',

  // Storage requirements
  packagingType: 'cardboard flats',
  temperatureRequired: {
    min: 10,
    max: 15,
    optimal: 12,
    unit: 'celsius'
  },
  shelfLifeHours: 168,  // 1 week

  // Seed saving!
  seedsSaved: true,
  seedBatchIdCreated: 'seeds-tomato-2024-002',  // Next generation!

  // Location
  fromLocation: {
    latitude: 40.7125,
    longitude: -74.0055,
    locationType: 'farm',
    facilityName: 'Brooklyn Urban Farm - Plot A'
  },
  toLocation: {
    latitude: 40.7128,
    longitude: -74.0060,
    locationType: 'warehouse',
    facilityName: 'Brooklyn Urban Farm - Pack House'
  },

  transportMethod: 'walking',
  distanceKm: 0.1,
  durationMinutes: 45,

  senderId: 'grower-brooklyn-001',
  receiverId: 'grower-brooklyn-001',
  status: 'verified'
};

const response = await fetch('/api/transport/harvest', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(harvestEvent)
});

const { data: block } = await response.json();
console.log('Harvest recorded:', block.hash);
console.log('Next generation seeds:', harvestEvent.seedBatchIdCreated);
```

## Step 5: View the Journey

### Get Complete Plant Journey

```typescript
// GET /api/transport/journey/plant-tomato-2024-001

const response = await fetch('/api/transport/journey/plant-tomato-2024-001');
const { data: journey } = await response.json();

console.log(journey);
/*
{
  plantId: 'plant-tomato-2024-001',
  seedBatchOrigin: 'seeds-tomato-2024-001',
  currentCustodian: 'grower-brooklyn-001',
  currentLocation: {
    facilityName: 'Brooklyn Urban Farm - Pack House',
    city: 'Brooklyn'
  },
  currentStage: 'post_harvest',

  events: [
    { eventType: 'seed_acquisition', ... },
    { eventType: 'planting', ... },
    { eventType: 'growing_transport', ... },
    { eventType: 'harvest', ... }
  ],

  totalFoodMiles: 350.25,  // km
  totalCarbonKg: 1.75,
  daysInTransit: 1,
  daysGrowing: 80,

  generation: 1,
  ancestorPlantIds: [],
  descendantSeedBatches: ['seeds-tomato-2024-002']
}
*/
```

### Get Environmental Impact

```typescript
// GET /api/transport/footprint/grower-brooklyn-001

const response = await fetch('/api/transport/footprint/grower-brooklyn-001');
const { data: impact } = await response.json();

console.log(impact);
/*
{
  totalCarbonKg: 1.75,
  totalFoodMiles: 350.25,
  carbonPerKgProduce: 0.042,  // Very low!

  comparisonToConventional: {
    carbonSaved: 103.25,  // kg CO2
    milesSaved: 62649.75,  // miles
    percentageReduction: 98  // Amazing!
  }
}
*/
```

### Generate QR Code

```typescript
// GET /api/transport/qr/plant-tomato-2024-001

const response = await fetch('/api/transport/qr/plant-tomato-2024-001');
const { data: qrData } = await response.json();

console.log(qrData);
/*
{
  plantId: 'plant-tomato-2024-001',
  blockchainAddress: '000abc123def456789...',
  quickLookupUrl: 'https://localgreenchain.org/track/plant-tomato-2024-001',
  lineageHash: 'a1b2c3d4e5f6',
  currentCustodian: 'grower-brooklyn-001',
  lastEventType: 'harvest',
  lastEventTimestamp: '2024-08-15T10:00:00Z',
  verificationCode: 'A1B2C3D4'
}
*/

// Consumer scans QR code → sees complete journey
```

## Complete Code Example

```typescript
// Full workflow in one script

import { TransportChain, getTransportChain } from '@/lib/transport/tracker';

async function seedToHarvestWorkflow() {
  // Initialize chain
  const chain = getTransportChain();

  // Step 1: Acquire seeds
  const seedEvent = createSeedAcquisitionEvent();
  const seedBlock = chain.recordEvent(seedEvent);
  console.log('Seeds acquired, block:', seedBlock.hash);

  // Step 2: Plant
  const plantEvent = createPlantingEvent(seedEvent.seedBatchId);
  const plantBlock = chain.recordEvent(plantEvent);
  console.log('Planted, block:', plantBlock.hash);

  // Step 3: Transplant (optional)
  const transplantEvent = createTransplantEvent(plantEvent.plantIds);
  const transplantBlock = chain.recordEvent(transplantEvent);
  console.log('Transplanted, block:', transplantBlock.hash);

  // Step 4: Harvest
  const harvestEvent = createHarvestEvent(plantEvent.plantIds);
  const harvestBlock = chain.recordEvent(harvestEvent);
  console.log('Harvested, block:', harvestBlock.hash);

  // Get journey for first plant
  const journey = chain.getPlantJourney(plantEvent.plantIds[0]);
  console.log('Journey:', journey);

  // Get environmental impact
  const impact = chain.getEnvironmentalImpact('grower-brooklyn-001');
  console.log('Impact:', impact);

  // Verify chain integrity
  const isValid = chain.isChainValid();
  console.log('Chain valid:', isValid);  // true

  return {
    seedBatchId: seedEvent.seedBatchId,
    plantIds: plantEvent.plantIds,
    harvestBatchId: harvestEvent.harvestBatchId,
    nextGeneration: harvestEvent.seedBatchIdCreated,
    journey,
    impact
  };
}

// Run it!
seedToHarvestWorkflow()
  .then(result => console.log('Complete!', result))
  .catch(console.error);
```

## What Happens Next?

### Distribution

```typescript
// Continue the chain with distribution...
POST /api/transport/distribution
{
  batchIds: ['harvest-tomato-2024-001'],
  destinationType: 'farmers_market',
  ...
}
```

### Next Generation

```typescript
// The cycle continues with saved seeds...
POST /api/transport/seed-acquisition
{
  seedBatchId: 'seeds-tomato-2024-002',  // From this harvest
  sourceType: 'previous_harvest',
  generation: 2,  // Second generation!
  geneticLineageId: 'lineage-cherokee-purple-001',
  parentPlantIds: ['plant-tomato-2024-001', 'plant-tomato-2024-005'],
  ...
}
```

The seed-to-seed cycle is complete!
