'use client'

import { useState } from 'react'
import { 
  DocumentTextIcon,
  CheckCircleIcon,
  ExclamationTriangleIcon
} from '@heroicons/react/24/outline'

interface TermsAndConditionsProps {
  systemInfo: any
  configData: any
  onComplete: (data: any) => void
  onBack?: () => void
  isCompleted: boolean
}

export default function TermsAndConditions({ 
  systemInfo, 
  configData, 
  onComplete, 
  onBack, 
  isCompleted 
}: TermsAndConditionsProps) {
  const [agreed, setAgreed] = useState(configData?.terms?.agreed || false)
  const [error, setError] = useState('')

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault()
    
    if (!agreed) {
      setError('You must agree to the Terms and Conditions to continue')
      return
    }

    setError('')
    onComplete({ 
      terms: { 
        agreed: true, 
        timestamp: new Date().toISOString() 
      } 
    })
  }

  return (
    <form onSubmit={handleSubmit} className="space-y-8">

      {/* Terms and Conditions Content */}
      <div className="card">
        <div className="flex items-center mb-4">
          <DocumentTextIcon className="h-6 w-6 text-ocean-500 mr-2" />
          <h3 className="text-lg font-medium text-chorus-text-primary">CHORUS:agents Software License Agreement</h3>
        </div>

        <div className="bg-chorus-warm border border-chorus-border-subtle rounded-lg p-6 max-h-96 overflow-y-auto">
          <div className="prose prose-sm max-w-none text-chorus-text-secondary">
            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">1. License Grant</h4>
            <p className="mb-4">
              Subject to the terms and conditions of this Agreement, Chorus Services grants you a non-exclusive, 
              non-transferable license to use CHORUS:agents (the "Software") for distributed AI coordination and task management.
            </p>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">2. Permitted Uses</h4>
            <ul className="list-disc list-inside mb-4 space-y-1">
              <li>Install and operate CHORUS:agents on your infrastructure</li>
              <li>Configure cluster nodes for distributed processing</li>
              <li>Integrate with supported AI models and services</li>
              <li>Use for commercial and non-commercial purposes</li>
            </ul>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">3. Restrictions</h4>
            <ul className="list-disc list-inside mb-4 space-y-1">
              <li>You may not redistribute, sublicense, or sell the Software</li>
              <li>You may not reverse engineer or decompile the Software</li>
              <li>You may not use the Software for illegal or harmful purposes</li>
              <li>You may not remove or modify proprietary notices</li>
            </ul>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">4. Data Privacy</h4>
            <p className="mb-4">
              CHORUS:agents processes data locally on your infrastructure. Chorus Services does not collect or store 
              your operational data. Telemetry data may be collected for software improvement purposes.
            </p>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">5. Support and Updates</h4>
            <p className="mb-4">
              Licensed users receive access to software updates, security patches, and community support. 
              Premium support tiers are available separately.
            </p>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">6. Disclaimer of Warranty</h4>
            <p className="mb-4">
              THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. CHORUS SERVICES DISCLAIMS 
              ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WARRANTIES OF MERCHANTABILITY AND FITNESS 
              FOR A PARTICULAR PURPOSE.
            </p>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">7. Limitation of Liability</h4>
            <p className="mb-4">
              IN NO EVENT SHALL CHORUS SERVICES BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL, 
              OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THE SOFTWARE.
            </p>

            <h4 className="text-base font-semibold text-chorus-text-primary mb-3">8. Termination</h4>
            <p className="mb-4">
              This license is effective until terminated. You may terminate it at any time by 
              uninstalling the Software. Chorus Services may terminate this license if you 
              violate any terms of this Agreement.
            </p>

            <div className="panel panel-info mt-6">
              <div className="flex">
                <ExclamationTriangleIcon className="h-5 w-5 text-ocean-600 dark:text-ocean-300 mt-0.5 mr-2" />
                <div className="text-sm panel-body">
                  <p><strong>Contact Information:</strong></p>
                  <p>Chorus Services<br />
                  Email: legal@chorus.services<br />
                  Website: https://chorus.services</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      {/* Agreement Checkbox */}
      <div className="card agreement">
        <div className="space-y-4">
          <label className="flex items-start">
            <input
              type="checkbox"
              checked={agreed}
              onChange={(e) => setAgreed(e.target.checked)}
              className="mt-1 mr-3 h-4 w-4 text-ocean-600 border-chorus-border-defined rounded focus:ring-ocean-600"
            />
            <div className="text-sm">
              <span className="font-medium text-chorus-text-primary">
                I have read and agree to the Terms and Conditions
              </span>
              <p className="text-chorus-text-secondary mt-1">
                By checking this box, you acknowledge that you have read, understood, and agree to be 
                bound by the terms and conditions outlined above.
              </p>
            </div>
          </label>

          {error && (
            <div className="flex items-center text-red-600 text-sm">
              <ExclamationTriangleIcon className="h-4 w-4 mr-1" />
              {error}
            </div>
          )}

          {agreed && (
            <div className="flex items-center text-eucalyptus-600 text-sm">
              <CheckCircleIcon className="h-4 w-4 mr-1" />
              Thank you for accepting the terms and conditions
            </div>
          )}
        </div>
      </div>

      <div className="flex justify-between pt-6 border-t border-chorus-border-defined">
        <div>
          {onBack && (
            <button type="button" onClick={onBack} className="btn-outline">
              Back
            </button>
          )}
        </div>
        <button 
          type="submit" 
          disabled={!agreed}
          className="btn-primary"
        >
{isCompleted ? 'Continue' : 'Next: License Validation'}
        </button>
      </div>
    </form>
  )
}
