import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import '../index.css';
import { createMcpApp } from '../lib/mcp-app-sdk';
import { ResultsViewer } from '../apps/ResultsViewer';

const app = createMcpApp();

// Mock data for standalone dev mode
const MOCK_DATA = {
  jobId: 'demo-001',
  status: 'complete' as const,
  properties: [
    {
      address: '1250 NW 7th St, Miami, FL 33125',
      propertyType: 'Multifamily',
      units: 24,
      sqft: 18500,
      yearBuilt: 1985,
      lotSize: '12,000 sqft',
      zoning: 'T5-O',
      owner: {
        name: 'James Rodriguez',
        company: 'Rodriguez Holdings LLC',
        portfolioSize: 12,
        phones: [
          { number: '(305) 555-0142', type: 'Mobile' },
          { number: '(305) 555-0198', type: 'Office' },
        ],
        emails: ['jrodriguez@rodriguezholdings.com', 'james.r@gmail.com'],
      },
      sales: [{ date: '2021-03-15', price: 3200000, buyer: 'Rodriguez Holdings LLC', seller: 'Sunset Properties Inc' }],
      debt: [{ lender: 'Wells Fargo', amount: 2400000, maturityDate: '2031-03-15', loanType: 'Fixed' }],
    },
    {
      address: '890 SW 1st Ave, Miami, FL 33130',
      propertyType: 'Office',
      units: 1,
      sqft: 45000,
      yearBuilt: 2002,
      lotSize: '22,000 sqft',
      zoning: 'T6-12',
      owner: {
        name: 'Sarah Chen',
        company: 'Brickell Capital Group',
        portfolioSize: 38,
        phones: [{ number: '(786) 555-0234', type: 'Office' }],
        emails: ['schen@brickellcap.com'],
      },
      sales: [{ date: '2019-08-22', price: 12500000, buyer: 'Brickell Capital Group', seller: 'Downtown Miami Partners' }],
    },
    {
      address: '425 NE 22nd St, Miami, FL 33137',
      propertyType: 'Multi Family (General)',
      units: 8,
      sqft: 6200,
      yearBuilt: 1962,
      owner: {
        name: 'Michael Torres',
        company: '',
        portfolioSize: 3,
        phones: [{ number: '(305) 555-0876', type: 'Mobile' }],
        emails: [],
      },
    },
    {
      address: '7700 NW 27th Ave, Miami, FL 33147',
      propertyType: 'Industrial',
      units: 1,
      sqft: 32000,
      yearBuilt: 1978,
      lotSize: '45,000 sqft',
      zoning: 'D1',
      owner: {
        name: 'Atlantic Warehousing Inc',
        company: 'Atlantic Warehousing Inc',
        portfolioSize: 7,
        phones: [{ number: '(305) 555-0345', type: 'Main' }],
        emails: ['info@atlanticwarehousing.com'],
      },
    },
    {
      address: '3200 Grand Ave, Coconut Grove, FL 33133',
      propertyType: 'Retail',
      units: 1,
      sqft: 8500,
      yearBuilt: 1995,
      owner: {
        name: 'Grove Retail Partners',
        company: 'Grove Retail Partners LLC',
        portfolioSize: 15,
        phones: [],
        emails: ['leasing@groveretail.com'],
      },
      sales: [{ date: '2023-01-10', price: 4800000 }],
    },
  ],
  totalCount: 5,
};

function ResultsEntry() {
  const [data, setData] = useState(MOCK_DATA);

  useEffect(() => {
    app.on('toolresult', (result: any) => {
      if (result?.properties) {
        setData(result);
      }
    });
  }, []);

  return <ResultsViewer data={data} />;
}

const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(<ResultsEntry />);

app.connect().catch(() => {
  console.log('[ResultsViewer] Running in standalone mode with mock data');
});
