import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
import { Badge } from "@/components/ui/badge"
import { Textarea } from "@/components/ui/textarea"
import { usePro } from "@/contexts/pro-context"
import { ProIndicator } from "@/components/pro/pro-indicator"
import { 
  RecaptchaState, 
  RecaptchaLocation, 
  recaptchaLocations,
  recaptchaThemes,
  recaptchaSizes,
  RecaptchaTheme,
  RecaptchaSize,
  captchaRoleOptions,
} from "../../config"

interface SettingsTabProps {
  settings: RecaptchaState
  toggleLocation: (locationId: RecaptchaLocation) => void
  toggleBypassRole: (role: string) => void
  updateSettings: <K extends keyof RecaptchaState>(key: K, value: RecaptchaState[K]) => void
}

export function SettingsTab({ settings, toggleLocation, toggleBypassRole, updateSettings }: SettingsTabProps) {
  const { isPro } = usePro()

  // Free locations are always shown
  const freeLocations = recaptchaLocations.filter(loc => !loc.isPro)
  // Pro locations only shown to Pro users
  const proLocations = recaptchaLocations.filter(loc => loc.isPro)

  return (
    <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
      {/* Left Column - Protected Pages */}
      <div className="space-y-6">
        {/* Protected Pages - Free Locations */}
        <Card>
          <CardHeader>
            <CardTitle>Protected Pages</CardTitle>
            <CardDescription>
              Select which pages should be protected by reCAPTCHA
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="grid gap-3">
              {freeLocations.map((location) => {
                const locationKey = location.id as RecaptchaLocation
                const isChecked = settings.locations[locationKey] || false
                
                return (
                  <div
                    key={location.id}
                    className={`
                      relative flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors
                      ${isChecked ? 'border-primary bg-primary/5' : 'border-muted hover:border-muted-foreground/50'}
                    `}
                    onClick={() => toggleLocation(locationKey)}
                  >
                    <Checkbox
                      id={location.id}
                      checked={isChecked}
                      onCheckedChange={() => toggleLocation(locationKey)}
                      className="mt-0.5"
                    />
                    <div className="flex-1 space-y-0.5">
                      <div className="flex items-center gap-2">
                        <Label 
                          htmlFor={location.id} 
                          className="font-medium cursor-pointer text-sm"
                        >
                          {location.name}
                        </Label>
                        {location.requiresWooCommerce && (
                          <Badge variant="outline" className="text-xs">
                            WooCommerce
                          </Badge>
                        )}
                      </div>
                      <p className="text-xs text-muted-foreground">
                        {location.description}
                      </p>
                    </div>
                  </div>
                )
              })}
            </div>
          </CardContent>
        </Card>

        {/* Pro Locations — hidden entirely for Free users */}
        {isPro && (
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                Additional Locations
                <ProIndicator size="sm" />
              </CardTitle>
              <CardDescription>
                Protect more forms with reCAPTCHA
              </CardDescription>
            </CardHeader>
            <CardContent>
              <div className="grid gap-3">
                {proLocations.map((location) => {
                  const locationKey = location.id as RecaptchaLocation
                  const isChecked = settings.locations[locationKey] || false
                  
                  return (
                    <div
                      key={location.id}
                      className={`
                        relative flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors
                        ${isChecked ? 'border-primary bg-primary/5' : 'border-muted hover:border-muted-foreground/50'}
                      `}
                      onClick={() => toggleLocation(locationKey)}
                    >
                      <Checkbox
                        id={location.id}
                        checked={isChecked}
                        onCheckedChange={() => toggleLocation(locationKey)}
                        className="mt-0.5"
                      />
                      <div className="flex-1 space-y-0.5">
                        <div className="flex items-center gap-2">
                          <Label 
                            htmlFor={location.id} 
                            className="font-medium cursor-pointer text-sm"
                          >
                            {location.name}
                          </Label>
                          {location.requiresWooCommerce && (
                            <Badge variant="outline" className="text-xs">
                              WooCommerce
                            </Badge>
                          )}
                        </div>
                        <p className="text-xs text-muted-foreground">
                          {location.description}
                        </p>
                      </div>
                    </div>
                  )
                })}
              </div>
            </CardContent>
          </Card>
        )}
      </div>

      {/* Right Column — Pro-only sections hidden entirely for Free users */}
      {isPro && (
        <div className="space-y-6">
          {/* Appearance Settings */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                Appearance
                <ProIndicator size="sm" />
              </CardTitle>
              <CardDescription>
                Customize the look of your reCAPTCHA widget
              </CardDescription>
            </CardHeader>
            <CardContent className="space-y-5">
              {/* Theme Selection */}
              <div className="space-y-2">
                <Label className="text-sm font-medium">Theme</Label>
                <div className="grid grid-cols-2 gap-2">
                  {recaptchaThemes.map((theme) => {
                    const isSelected = settings.theme === theme.id
                    
                    return (
                      <div
                        key={theme.id}
                        className={`
                          relative flex items-center gap-2 rounded-lg border p-2.5 cursor-pointer transition-colors
                          ${isSelected ? 'border-primary bg-primary/5' : 'border-muted hover:border-muted-foreground/50'}
                        `}
                        onClick={() => updateSettings('theme', theme.id as RecaptchaTheme)}
                      >
                        <div className={`w-5 h-5 rounded border-2 ${theme.id === 'dark' ? 'bg-gray-800 border-gray-600' : 'bg-white border-gray-300'}`} />
                        <div className="flex-1">
                          <span className="font-medium text-sm">{theme.name}</span>
                        </div>
                      </div>
                    )
                  })}
                </div>
              </div>

              {/* Size Selection */}
              <div className="space-y-2">
                <Label className="text-sm font-medium">Size</Label>
                <div className="grid grid-cols-2 gap-2">
                  {recaptchaSizes.map((size) => {
                    const isSelected = settings.size === size.id
                    
                    return (
                      <div
                        key={size.id}
                        className={`
                          relative flex items-center gap-2 rounded-lg border p-2.5 cursor-pointer transition-colors
                          ${isSelected ? 'border-primary bg-primary/5' : 'border-muted hover:border-muted-foreground/50'}
                        `}
                        onClick={() => updateSettings('size', size.id as RecaptchaSize)}
                      >
                        <div className={`rounded border-2 border-gray-300 ${size.id === 'compact' ? 'w-4 h-3' : 'w-5 h-4'} bg-gray-100`} />
                        <div className="flex-1">
                          <span className="font-medium text-sm">{size.name}</span>
                        </div>
                      </div>
                    )
                  })}
                </div>
              </div>
            </CardContent>
          </Card>

          {/* IP Whitelist */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                IP Whitelist
                <ProIndicator size="sm" />
              </CardTitle>
              <CardDescription>
                Disable reCAPTCHA for specific IP addresses or ranges
              </CardDescription>
            </CardHeader>
            <CardContent className="space-y-3">
              <div className="space-y-2">
                <Label htmlFor="ipWhitelist">IP Addresses / Ranges</Label>
                <Textarea
                  id="ipWhitelist"
                  placeholder={"Enter IP addresses (one per line)\nExamples:\n192.168.1.1\n10.0.0.0/24"}
                  value={settings.ipWhitelist}
                  onChange={(e) => updateSettings('ipWhitelist', e.target.value)}
                  className="min-h-[100px] font-mono text-sm"
                />
                <p className="text-xs text-muted-foreground">
                  Enter one IP address or range per line. Supports CIDR notation.
                </p>
              </div>
            </CardContent>
          </Card>

          {/* Role Bypass */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                Role Bypass
                <ProIndicator size="sm" />
              </CardTitle>
              <CardDescription>
                Skip CAPTCHA verification for logged-in users with specific roles
              </CardDescription>
            </CardHeader>
            <CardContent>
              <div className="grid gap-3">
                {captchaRoleOptions.map((role) => {
                  const isChecked = settings.bypassRoles.includes(role.value)

                  return (
                    <div
                      key={role.value}
                      className={`
                        relative flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors
                        ${isChecked ? 'border-primary bg-primary/5' : 'border-muted hover:border-muted-foreground/50'}
                      `}
                      onClick={() => toggleBypassRole(role.value)}
                    >
                      <Checkbox
                        id={`bypass-${role.value}`}
                        checked={isChecked}
                        onCheckedChange={() => toggleBypassRole(role.value)}
                        className="mt-0.5"
                      />
                      <Label
                        htmlFor={`bypass-${role.value}`}
                        className="font-medium cursor-pointer text-sm"
                      >
                        {role.label}
                      </Label>
                    </div>
                  )
                })}
              </div>
              <p className="text-xs text-muted-foreground mt-3">
                Selected roles will not see CAPTCHA challenges when logged in.
              </p>
            </CardContent>
          </Card>
        </div>
      )}
    </div>
  )
}
