import React, { useState } from 'react';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Checkbox } from "@/components/ui/checkbox";
import { X } from "lucide-react";

const PropertyTable = ({
  properties = [],
  onChange,
  propertyName = ''
}) => {
  const [localProperties, setLocalProperties] = useState(properties);

  // Handle property changes without causing infinite loops
  const handlePropertyChange = (index, field, value) => {
    const updatedProperties = localProperties.map((prop, i) => {
      if (i === index) {
        return { ...prop, [field]: value };
      }
      return prop;
    });
    setLocalProperties(updatedProperties);
    onChange?.(updatedProperties);
  };

  // Add new property
  const handleAdd = () => {
    const newProperty = {
      enabled: true,
      key: '',
      value: ''
    };
    const updatedProperties = [...localProperties, newProperty];
    setLocalProperties(updatedProperties);
    onChange?.(updatedProperties);
  };

  // Remove property
  const handleRemove = (index) => {
    const updatedProperties = localProperties.filter((_, i) => i !== index);
    setLocalProperties(updatedProperties);
    onChange?.(updatedProperties);
  };

  return (
      <div className="space-y-4">
        <div className="flex justify-between items-center">
          <h3 className="text-lg font-medium">{propertyName}</h3>

        </div>

        <Table>
          <TableHeader>
            <TableRow>
              <TableHead className="w-14">Enable</TableHead>
              <TableHead>Key</TableHead>
              <TableHead>Value</TableHead>
              <TableHead className="w-14">
                <Button
                    onClick={handleAdd}
                    variant="outline"
                    size="sm"
                    className="w-20"
                >
                  Add
                </Button>
              </TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {localProperties.map((property, index) => (
                <TableRow key={index}>
                  <TableCell className="">
                    <Checkbox
                        checked={property.enabled}
                        onCheckedChange={(checked) =>
                            handlePropertyChange(index, 'enabled', checked)
                        }
                    />
                  </TableCell>
                  <TableCell>
                    <Input
                        value={property.key}
                        onChange={(e) =>
                            handlePropertyChange(index, 'key', e.target.value)
                        }
                        className="w-full"
                    />
                  </TableCell>
                  <TableCell>
                    <Input
                        value={property.value}
                        onChange={(e) =>
                            handlePropertyChange(index, 'value', e.target.value)
                        }
                        className="w-full"
                    />
                  </TableCell>
                  <TableCell>
                    <Button
                        variant="ghost"
                        size="icon"
                        onClick={() => handleRemove(index)}
                        className="h-8 w-8"
                    >
                      <X className="h-4 w-4" />
                    </Button>
                  </TableCell>
                </TableRow>
            ))}
          </TableBody>
        </Table>
      </div>
  );
};

export default PropertyTable;
