<script setup>
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/Components/ui/card";
import { Button } from "@/Components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/Components/ui/dialog";
import { Input } from "@/Components/ui/input";
import { Label } from "@/Components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/Components/ui/select";
import { Checkbox } from "@/Components/ui/checkbox";
import { Badge } from "@/Components/ui/badge";
import { useForm, router } from "@inertiajs/vue3";
import { ref } from "vue";
import { Plus, Pencil, Trash } from "lucide-vue-next";

const props = defineProps({
  report: Object,
  filters: Array,
});

const showCreateDialog = ref(false);
const showEditDialog = ref(false);
const editingFilter = ref(null);

const createForm = useForm({
  key: "",
  label: "",
  type: "string",
  nullable: true,
  default_value: "",
  data_source: "",
  order: 0,
});

const editForm = useForm({
  key: "",
  label: "",
  type: "string",
  nullable: true,
  default_value: "",
  data_source: "",
  order: 0,
});

function openCreateDialog() {
  createForm.reset();
  createForm.order = props.filters.length;
  showCreateDialog.value = true;
}

function submitCreate() {
  createForm.post(route("settings.reports.filters.store", props.report.id), {
    preserveScroll: true,
    onSuccess: () => {
      showCreateDialog.value = false;
      createForm.reset();
    },
  });
}

function openEditDialog(filter) {
  editingFilter.value = filter;
  editForm.key = filter.key;
  editForm.label = filter.label;
  editForm.type = filter.type;
  editForm.nullable = filter.nullable;
  editForm.default_value = filter.default_value || "";
  editForm.data_source = filter.data_source || "";
  editForm.order = filter.order;
  showEditDialog.value = true;
}

function submitEdit() {
  editForm.put(route("settings.reports.filters.update", editingFilter.value.id), {
    preserveScroll: true,
    onSuccess: () => {
      showEditDialog.value = false;
      editForm.reset();
      editingFilter.value = null;
    },
  });
}

function deleteFilter(filter) {
  if (confirm("Are you sure you want to delete this filter?")) {
    router.delete(route("settings.reports.filters.destroy", filter.id), {
      preserveScroll: true,
    });
  }
}
</script>

<template>
  <Card>
    <CardHeader>
      <div class="flex items-start justify-between">
        <div>
          <CardTitle>Report Filters</CardTitle>
          <CardDescription>
            Define input parameters that users can provide to filter the report
          </CardDescription>
        </div>
        <Button @click="openCreateDialog">
          <Plus class="mr-2 h-4 w-4" />
          Add Filter
        </Button>
      </div>
    </CardHeader>
    <CardContent>
      <div v-if="filters.length === 0" class="text-center py-8 text-gray-500">
        No filters configured. Add filters to allow users to filter report results.
      </div>

      <div v-else class="space-y-3">
        <div
          v-for="filter in filters"
          :key="filter.id"
          class="flex items-start justify-between rounded-lg border p-4"
        >
          <div class="flex-1">
            <div class="flex items-center gap-2 mb-2">
              <span class="font-semibold">{{ filter.label }}</span>
              <Badge variant="outline">{{ filter.type }}</Badge>
              <Badge v-if="filter.nullable" variant="secondary">nullable</Badge>
              <Badge v-if="filter.data_source" variant="secondary">{{ filter.data_source }}</Badge>
            </div>
            <div class="text-sm text-gray-600 dark:text-gray-400 font-mono">
              {{ filter.key }}
            </div>
            <div v-if="filter.default_value" class="text-sm text-gray-500 mt-1">
              Default: {{ filter.default_value }}
            </div>
            <div class="text-xs text-gray-500 mt-1">Order: {{ filter.order }}</div>
          </div>
          <div class="flex gap-2">
            <Button variant="ghost" size="icon" @click="openEditDialog(filter)">
              <Pencil class="h-4 w-4" />
            </Button>
            <Button variant="ghost" size="icon" @click="deleteFilter(filter)">
              <Trash class="h-4 w-4 text-destructive" />
            </Button>
          </div>
        </div>
      </div>
    </CardContent>
  </Card>

  <!-- Create Dialog -->
  <Dialog v-model:open="showCreateDialog">
    <DialogContent class="max-w-2xl">
      <DialogHeader>
        <DialogTitle>Add Filter</DialogTitle>
        <DialogDescription>
          Add a new filter parameter for the report
        </DialogDescription>
      </DialogHeader>

      <form @submit.prevent="submitCreate" class="space-y-4">
        <div class="grid grid-cols-2 gap-4">
          <div class="space-y-2">
            <Label for="create-key">Key *</Label>
            <Input
              id="create-key"
              v-model="createForm.key"
              placeholder="client_uuid"
              required
            />
          </div>

          <div class="space-y-2">
            <Label for="create-label">Label *</Label>
            <Input
              id="create-label"
              v-model="createForm.label"
              placeholder="Client"
              required
            />
          </div>
        </div>

        <div class="space-y-2">
          <Label for="create-type">Type *</Label>
          <Select v-model="createForm.type">
            <SelectTrigger>
              <SelectValue />
            </SelectTrigger>
            <SelectContent>
              <SelectItem value="string">string</SelectItem>
              <SelectItem value="date">date</SelectItem>
              <SelectItem value="number">number</SelectItem>
              <SelectItem value="boolean">boolean</SelectItem>
              <SelectItem value="select">select</SelectItem>
              <SelectItem value="select:client">select:client</SelectItem>
              <SelectItem value="select:user">select:user</SelectItem>
              <SelectItem value="multiselect">multiselect</SelectItem>
            </SelectContent>
          </Select>
        </div>

        <div class="space-y-2">
          <Label for="create-data-source">Data Source (optional)</Label>
          <Input
            id="create-data-source"
            v-model="createForm.data_source"
            placeholder="clients, users, segments..."
          />
          <p class="text-xs text-gray-500">For dynamic selects</p>
        </div>

        <div class="space-y-2">
          <Label for="create-default">Default Value (optional)</Label>
          <Input
            id="create-default"
            v-model="createForm.default_value"
            placeholder="Default value..."
          />
        </div>

        <div class="flex items-center space-x-2">
          <Checkbox
            id="create-nullable"
            v-model="createForm.nullable"
          />
          <Label for="create-nullable" class="cursor-pointer">
            Nullable (filter is optional)
          </Label>
        </div>

        <div class="space-y-2">
          <Label for="create-order">Order</Label>
          <Input
            id="create-order"
            v-model.number="createForm.order"
            type="number"
          />
        </div>

        <DialogFooter>
          <Button type="button" variant="outline" @click="showCreateDialog = false">
            Cancel
          </Button>
          <Button type="submit" :disabled="createForm.processing">
            Add Filter
          </Button>
        </DialogFooter>
      </form>
    </DialogContent>
  </Dialog>

  <!-- Edit Dialog -->
  <Dialog v-model:open="showEditDialog">
    <DialogContent class="max-w-2xl">
      <DialogHeader>
        <DialogTitle>Edit Filter</DialogTitle>
        <DialogDescription>
          Update filter configuration
        </DialogDescription>
      </DialogHeader>

      <form @submit.prevent="submitEdit" class="space-y-4">
        <div class="grid grid-cols-2 gap-4">
          <div class="space-y-2">
            <Label for="edit-key">Key *</Label>
            <Input
              id="edit-key"
              v-model="editForm.key"
              required
            />
          </div>

          <div class="space-y-2">
            <Label for="edit-label">Label *</Label>
            <Input
              id="edit-label"
              v-model="editForm.label"
              required
            />
          </div>
        </div>

        <div class="space-y-2">
          <Label for="edit-type">Type *</Label>
          <Select v-model="editForm.type">
            <SelectTrigger>
              <SelectValue />
            </SelectTrigger>
            <SelectContent>
              <SelectItem value="string">string</SelectItem>
              <SelectItem value="date">date</SelectItem>
              <SelectItem value="number">number</SelectItem>
              <SelectItem value="boolean">boolean</SelectItem>
              <SelectItem value="select">select</SelectItem>
              <SelectItem value="select:client">select:client</SelectItem>
              <SelectItem value="select:user">select:user</SelectItem>
              <SelectItem value="multiselect">multiselect</SelectItem>
            </SelectContent>
          </Select>
        </div>

        <div class="space-y-2">
          <Label for="edit-data-source">Data Source (optional)</Label>
          <Input
            id="edit-data-source"
            v-model="editForm.data_source"
          />
        </div>

        <div class="space-y-2">
          <Label for="edit-default">Default Value (optional)</Label>
          <Input
            id="edit-default"
            v-model="editForm.default_value"
          />
        </div>

        <div class="flex items-center space-x-2">
          <Checkbox
            id="edit-nullable"
            v-model="editForm.nullable"
          />
          <Label for="edit-nullable" class="cursor-pointer">
            Nullable (filter is optional)
          </Label>
        </div>

        <div class="space-y-2">
          <Label for="edit-order">Order</Label>
          <Input
            id="edit-order"
            v-model.number="editForm.order"
            type="number"
          />
        </div>

        <DialogFooter>
          <Button type="button" variant="outline" @click="showEditDialog = false">
            Cancel
          </Button>
          <Button type="submit" :disabled="editForm.processing">
            Update Filter
          </Button>
        </DialogFooter>
      </form>
    </DialogContent>
  </Dialog>
</template>
