"use client";

import { z } from "zod";
import { useForm, Controller } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "../ui/dialog";
import { Checkbox } from "../ui/checkbox";
import { Button } from "../ui/button";
import { Textarea } from "../ui/textarea";
import { useState } from "react";
import { createTaskTa } from "@/service/task";
import { createSurveyData } from "@/service/survey/survey";

const surveySchema = z.object({
  accessFrequency: z.string(),
  uiExperienceDesign: z.string(),
  uiExperienceNavigation: z.string(),
  uiExperienceSpeed: z.string(),
  infoAccuracy: z.string(),
  infoCompleteness: z.string(),
  usefulness: z.string(),
  suggestion: z.string().optional(),
});

type SurveySchema = z.infer<typeof surveySchema>;

export default function FormSurvey() {
  const [showSurvey, setShowSurvey] = useState(true);
  const [isLoading, setIsLoading] = useState(false);
  const {
    control,
    handleSubmit,
    formState: { errors },
  } = useForm<SurveySchema>({
    resolver: zodResolver(surveySchema),
    mode: "all",
    defaultValues: {
      accessFrequency: "",
      uiExperienceDesign: "",
      uiExperienceNavigation: "",
      uiExperienceSpeed: "",
      infoAccuracy: "",
      infoCompleteness: "",
      usefulness: "",
      suggestion: "",
    },
  });

  const options = {
    accessFrequency: [
      "Setiap hari",
      "Beberapa kali seminggu",
      "Beberapa kali dalam sebulan",
      "Baru pertama kali",
    ],
    uiExperienceDesign: ["Sangat baik", "Baik", "Cukup", "Kurang", "Buruk"],
    uiExperienceNavigation: [
      "Sangat mudah",
      "Mudah",
      "Cukup",
      "Sulit",
      "Sangat sulit",
    ],
    uiExperienceSpeed: [
      "Sangat cepat",
      "Cepat",
      "Cukup",
      "Lambat",
      "Sangat lambat",
    ],
    infoAccuracy: ["Sangat puas", "Puas", "Cukup", "Kurang puas", "Tidak puas"],
    infoCompleteness: [
      "Sangat lengkap",
      "Lengkap",
      "Cukup",
      "Kurang lengkap",
      "Tidak lengkap",
    ],
    usefulness: [
      "Sangat membantu",
      "Membantu",
      "Cukup membantu",
      "Kurang membantu",
      "Tidak membantu",
    ],
  };

  const renderControllerGroup = (
    name: keyof SurveySchema,
    question: string,
    choices: string[]
  ) => (
    <div className="space-y-2">
      <p className="font-medium">{question}</p>
      <div className="grid grid-cols-2 gap-2">
        {choices.map((choice, i) => (
          <Controller
            key={i}
            name={name}
            control={control}
            render={({ field }) => (
              <label className="flex items-center space-x-2">
                <Checkbox
                  checked={field.value === choice}
                  onCheckedChange={() => field.onChange(choice)}
                />
                <span>{choice}</span>
              </label>
            )}
          />
        ))}
      </div>
      {errors[name] && (
        <p className="text-red-500 text-sm">
          {errors[name]?.message as string}
        </p>
      )}
    </div>
  );

  const onSubmit = async (data: SurveySchema) => {
    setIsLoading(true);
    try {
      const response = await createSurveyData(data);
      console.log("API Response:", response);
      setShowSurvey(false);
    } catch (error) {
      console.error("Error submitting survey:", error);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <Dialog open={showSurvey} onOpenChange={setShowSurvey}>
      <DialogContent className="z-50 min-w-max h-[600px] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="text-lg font-bold">
            SURVEI KEPUASAN PENGGUNA MEDIAHUB POLRI
          </DialogTitle>
          <DialogDescription className="text-sm">
            Kami menghargai pendapat Anda! Survei ini bertujuan untuk
            meningkatkan kualitas layanan MediaHub Polri.
          </DialogDescription>
        </DialogHeader>

        <form onSubmit={handleSubmit(onSubmit)} className="space-y-6 mt-4">
          {renderControllerGroup(
            "accessFrequency",
            "1. Seberapa sering Anda mengakses MediaHub Polri?",
            options.accessFrequency
          )}

          <div>
            <p className="font-medium">
              2. Bagaimana pengalaman Anda dalam mengakses website ini?
            </p>
            <div className="space-y-3 mt-2">
              {renderControllerGroup(
                "uiExperienceDesign",
                "a) Tampilan dan desain website",
                options.uiExperienceDesign
              )}
              {renderControllerGroup(
                "uiExperienceNavigation",
                "b) Kemudahan navigasi",
                options.uiExperienceNavigation
              )}
              {renderControllerGroup(
                "uiExperienceSpeed",
                "c) Kecepatan akses website",
                options.uiExperienceSpeed
              )}
            </div>
          </div>

          <div>
            <p className="font-medium">
              3. Seberapa puas Anda dengan informasi yang tersedia di MediaHub
              Polri?
            </p>
            <div className="space-y-3 mt-2">
              {renderControllerGroup(
                "infoAccuracy",
                "a) Akurat dan terpercaya",
                options.infoAccuracy
              )}
              {renderControllerGroup(
                "infoCompleteness",
                "b) Kelengkapan berita dan informasi",
                options.infoCompleteness
              )}
            </div>
          </div>

          {renderControllerGroup(
            "usefulness",
            "4. Apakah Anda merasa website ini membantu dalam mendapatkan informasi terkait Polri?",
            options.usefulness
          )}

          <div>
            <p className="font-medium">5. Apa saran atau masukan Anda?</p>
            <Controller
              name="suggestion"
              control={control}
              render={({ field }) => (
                <Textarea
                  placeholder="Tulis pesan Anda..."
                  value={field.value}
                  onChange={field.onChange}
                />
              )}
            />
          </div>

          <div className="flex justify-end gap-2">
            <Button variant="outline" onClick={() => setShowSurvey(false)}>
              Batal
            </Button>
            <Button type="submit" disabled={isLoading}>
              {isLoading ? "Mengirim..." : "Kirim"}
            </Button>
          </div>
        </form>
      </DialogContent>
    </Dialog>
  );
}
