{
  "name": "Solar Site Screener - Automated Parcel Analysis",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "solar-screener",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300],
      "webhookId": "solar-screener-webhook"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT \n  parcel_id,\n  ST_X(ST_Centroid(geom)) as lon,\n  ST_Y(ST_Centroid(geom)) as lat,\n  acres,\n  zoning,\n  owner\nFROM parcels\nWHERE county_fips = '{{ $json.county_fips }}'\n  AND acres >= {{ $json.min_acres | 10 }}\nLIMIT {{ $json.limit | 100 }}"
      },
      "id": "fetch-parcels",
      "name": "Fetch Parcels from PostGIS",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [450, 300],
      "credentials": {
        "postgres": {
          "id": "1",
          "name": "PostGIS Database"
        }
      }
    },
    {
      "parameters": {
        "url": "https://developer.nrel.gov/api/solar/solar_resource/v1.json",
        "authentication": "none",
        "options": {
          "queryParameters": {
            "parameters": [
              {
                "name": "api_key",
                "value": "={{ $env.NREL_API_KEY }}"
              },
              {
                "name": "lat",
                "value": "={{ $json.lat }}"
              },
              {
                "name": "lon",
                "value": "={{ $json.lon }}"
              }
            ]
          }
        }
      },
      "id": "nrel-solar-data",
      "name": "Fetch NREL Solar Data",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [650, 300]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT fld_zone\nFROM fema_flood_zones\nWHERE ST_Intersects(\n  geom,\n  (SELECT geom FROM parcels WHERE parcel_id = '{{ $json.parcel_id }}')\n)\nLIMIT 1"
      },
      "id": "check-flood-zone",
      "name": "Check Flood Zone",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [650, 450],
      "credentials": {
        "postgres": {
          "id": "1",
          "name": "PostGIS Database"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT \n  ST_Distance(\n    (SELECT geom FROM parcels WHERE parcel_id = '{{ $json.parcel_id }}'),\n    geom\n  ) / 1609.34 as distance_miles\nFROM transmission_lines\nWHERE voltage_kv >= 230\nORDER BY distance_miles\nLIMIT 1"
      },
      "id": "transmission-distance",
      "name": "Distance to Transmission",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [650, 600],
      "credentials": {
        "postgres": {
          "id": "1",
          "name": "PostGIS Database"
        }
      }
    },
    {
      "parameters": {
        "model": "claude-3-5-sonnet-20241022",
        "options": {
          "maxTokens": 1024
        }
      },
      "id": "claude-scoring",
      "name": "Claude AI Suitability Scoring",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1,
      "position": [850, 400],
      "credentials": {
        "anthropicApi": {
          "id": "2",
          "name": "Anthropic Claude API"
        }
      },
      "inputData": {
        "prompt": "Score this parcel for solar development (0-100) and provide recommendations:\n\nParcel Details:\n- ID: {{ $json.parcel_id }}\n- Size: {{ $json.acres }} acres\n- Zoning: {{ $json.zoning }}\n- Solar DNI: {{ $json.solar_dni }} kWh/m²/day\n- Flood Zone: {{ $json.flood_zone || 'None' }}\n- Distance to Transmission: {{ $json.distance_to_transmission }} miles\n\nProvide response as JSON:\n{\n  \"suitability_score\": 0-100,\n  \"category\": \"Excellent|Good|Fair|Poor\",\n  \"key_constraints\": [],\n  \"recommendations\": [],\n  \"estimated_capacity_mw\": number\n}"
      }
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "parcel_analysis_results",
        "columns": "parcel_id,suitability_score,category,solar_dni,flood_zone,distance_to_transmission,ai_analysis,analyzed_at"
      },
      "id": "save-results",
      "name": "Save Results to Database",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [1050, 400],
      "credentials": {
        "postgres": {
          "id": "1",
          "name": "PostGIS Database"
        }
      }
    },
    {
      "parameters": {
        "operation": "sendEmail",
        "fromEmail": "no-reply@geospatialsolutions.co",
        "toEmail": "={{ $json.client_email }}",
        "subject": "Solar Site Analysis Complete - {{ $json.county_name }}",
        "emailFormat": "html",
        "html": "<h2>Your Solar Site Analysis is Ready</h2>\n<p>We've analyzed {{ $json.total_parcels }} parcels in {{ $json.county_name }}.</p>\n<h3>Top Prospects:</h3>\n<ul>\n{{ $json.top_parcels }}\n</ul>\n<p><a href=\"{{ $json.dashboard_url }}\">View Interactive Dashboard</a></p>",
        "options": {}
      },
      "id": "send-email",
      "name": "Email Results",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [1050, 550],
      "credentials": {
        "smtp": {
          "id": "3",
          "name": "SendGrid SMTP"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { \"status\": \"success\", \"analyzed\": $json.total_parcels, \"dashboard_url\": $json.dashboard_url } }}"
      },
      "id": "respond",
      "name": "Send Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1250, 400]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Fetch Parcels from PostGIS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Parcels from PostGIS": {
      "main": [
        [
          {
            "node": "Fetch NREL Solar Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Flood Zone",
            "type": "main",
            "index": 0
          },
          {
            "node": "Distance to Transmission",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch NREL Solar Data": {
      "main": [
        [
          {
            "node": "Claude AI Suitability Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Flood Zone": {
      "main": [
        [
          {
            "node": "Claude AI Suitability Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Distance to Transmission": {
      "main": [
        [
          {
            "node": "Claude AI Suitability Scoring",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude AI Suitability Scoring": {
      "main": [
        [
          {
            "node": "Save Results to Database",
            "type": "main",
            "index": 0
          },
          {
            "node": "Email Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Results": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "solar",
      "id": "solar-automation"
    },
    {
      "name": "gis",
      "id": "gis-analysis"
    }
  ],
  "meta": {
    "instanceId": "geospatial-solutions-template"
  },
  "pinData": {},
  "versionId": "1.0"
}
