"use client";

import React, { useEffect, useState } from "react";
import {
  Button,
  Group,
  Paper,
  Radio,
  ScrollArea,
  Select,
  Stack,
  Text,
  TextInput,
  Title,
} from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { useRouter } from "next/navigation";

interface accountData {
  stAccountNo: string;
  stAccountType: string;
  stAvailableBalance: string;
  custname: string;
}

export default function QuickPay() {
  const router = useRouter();
  const [bankType, setBankType] = useState("own");
  const [authorized, setAuthorized] = useState<boolean | null>(null);
  const [accountData, setAccountData] = useState<accountData[]>([]);
  const [selectedAccNo, setSelectedAccNo] = useState<string | null>(null);
  const [beneficiaryAcc, setBeneficiaryAcc] = useState("");
  const [confirmBeneficiaryAcc, setConfirmBeneficiaryAcc] = useState("");
  const [beneficiaryType, setBeneficiaryType] = useState<string | null>(null);
  const [isVisibilityLocked, setIsVisibilityLocked] = useState(false);
  const [amount, setAmount] = useState("");
  const [remarks, setRemarks] = useState("");
  const [showTxnPassword, setShowTxnPassword] = useState(false);
  const [txnPassword, setTxnPassword] = useState("");
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [validationStatus, setValidationStatus] = useState<"success" | "error" | null>(null);
  const [beneficiaryName, setBeneficiaryName] = useState<string | null>(null);
  const [showOtpField, setShowOtpField] = useState(false);
  const [otp, setOtp] = useState("");

  const accountOptions = accountData.map((acc) => ({
    value: acc.stAccountNo,
    label: `${acc.stAccountNo} (${acc.stAccountType})`,
  }));

  const FetchAccountDetails = async () => {
    try {
      const token = localStorage.getItem("access_token");
      const response = await fetch("/api/customer", {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
      });
      const data = await response.json();
      if (response.ok && Array.isArray(data)) {
        const filterSAaccount =data.filter((acc)=>acc.stAccountType==='SA');
        setAccountData(filterSAaccount);
      }
    } catch {
      notifications.show({
        withBorder: true,
        color: "red",
        title: "Please try again later",
        message: "Unable to Fetch, Please try again later",
        autoClose: 5000,
      });
    }
  };

  useEffect(() => {
    const token = localStorage.getItem("access_token");
    if (!token) {
      setAuthorized(false);
      router.push("/login");
    } else {
      setAuthorized(true);
    }
  }, []);

  useEffect(() => {
    if (authorized) {
      FetchAccountDetails();
    }
  }, [authorized]);

  const isValidTxnPassword = (password: string) =>
    /[A-Z]/i.test(password) && /[0-9]/.test(password) && /[^a-zA-Z0-9]/.test(password);

  const handleValidate = async () => {
    if (!selectedAccNo || !beneficiaryAcc ||
      !confirmBeneficiaryAcc
    ) {
      notifications.show({
        title: "Validation Error",
        message: "Please fill debit account, beneficiary account number and confirm beneficiary account number",
        color: "red",
      });
      return;
    }
    if (beneficiaryAcc.length < 10 || beneficiaryAcc.length > 17) {
      notifications.show({
        title: "Invalid Account Number",
        message: "Please Enter valid account Number",
        color: "red",
      });
      return;
    }
    if (beneficiaryAcc !== confirmBeneficiaryAcc) {
      notifications.show({
        title: "Mismatch",
        message: "Beneficiary account numbers do not match",
        color: "red",
      });
      return;
    }

    try {
      const token = localStorage.getItem("access_token");
      const response = await fetch(`/api/beneficiary/validate/within-bank?accountNumber=${beneficiaryAcc}`, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
      });
      const data = await response.json();

      if (response.ok && data?.name) {
        setBeneficiaryName(data.name);
        setValidationStatus("success");
        setIsVisibilityLocked(true);
      } else {
        setBeneficiaryName("Invalid account number");
        setValidationStatus("error");
      }
    } catch {
      setBeneficiaryName("Invalid account number");
      setValidationStatus("error");
    }
  };

  const handleProceed = async () => {
    if (!selectedAccNo || !beneficiaryAcc || !confirmBeneficiaryAcc || !beneficiaryType || !amount || !remarks) {
      notifications.show({
        title: "Validation Error",
        message: "Please fill all required fields",
        color: "red",
      });
      return;
    }

    if (validationStatus !== "success") {
      notifications.show({
        title: "Validation Required",
        message: "Please validate beneficiary before proceeding",
        color: "red",
      });
      return;
    }

    if (!showTxnPassword) {
      setShowTxnPassword(true);
      return;
    }

    if (!txnPassword || !isValidTxnPassword(txnPassword)) {
      notifications.show({
        title: "Weak Password",
        message: "Password must contain letter, number, and special character",
        color: "red",
      });
      return;
    }

    if (!showOtpField) {
      setShowOtpField(true);
      notifications.show({
        title: "OTP Sent",
        message: "Check your registered device for OTP",
        color: "green",
      });
      return;
    }

    if (!otp) {
      notifications.show({
        title: "Enter OTP",
        message: "Please enter the OTP",
        color: "red",
      });
      return;
    }

    try {
      setIsSubmitting(true);
      const token = localStorage.getItem("access_token");

      const res = await fetch("/api/payment/transfer", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
        body: JSON.stringify({
          fromAccount: selectedAccNo,
          toAccount: beneficiaryAcc,
          toAccountType: beneficiaryType,
          amount,
          narration: remarks,
          txnPassword,
          otp,
        }),
      });

      const result = await res.json();

      if (res.ok) {
        notifications.show({
          title: "Success",
          message: "Transaction successful",
          color: "green",
        });

        // Reset
        setShowTxnPassword(false);
        setTxnPassword("");
        setShowOtpField(false);
        setOtp("");
        setValidationStatus(null);
        setBeneficiaryName(null);
      } else {
        notifications.show({
          title: "Error",
          message: result?.message || "Transaction failed",
          color: "red",
        });
      }
    } catch {
      notifications.show({
        title: "Error",
        message: "Something went wrong",
        color: "red",
      });
    } finally {
      setIsSubmitting(false);
    }
  };

  if (!authorized) return null;

  return (
    <Paper shadow="sm" radius="md" p="md" withBorder h={400}>
      <Title order={3} mb="md">
        Quick Pay
      </Title>
      <Radio.Group value={bankType} onChange={setBankType} name="bankType" withAsterisk mb="md">
        <Group justify="center">
          <Radio value="own" label="Own Bank" />
          <Radio value="outside" label="Outside Bank" />
        </Group>
      </Radio.Group>

      {bankType === "own" ? (
        <ScrollArea style={{ flex: 1 }}>
          <div style={{ maxHeight: "320px", overflowY: "auto" }}>
            <Stack gap="xs">
              <Group grow>
                <Select
                  label="Select Debit Account Number"
                  placeholder="Choose account number"
                  data={accountOptions}
                  value={selectedAccNo}
                  onChange={setSelectedAccNo}
                  withAsterisk
                  disabled={isVisibilityLocked}
                />

                <TextInput
                  label="Beneficiary Account No"
                  value={beneficiaryAcc}
                  onChange={(e) => {
                    const value = e.currentTarget.value;
                    if (/^\d*$/.test(value)) {
                      setBeneficiaryAcc(value);
                    }
                  }}
                  withAsterisk
                  disabled={isVisibilityLocked}
                />

                <TextInput
                  label="Confirm Beneficiary Account No"
                  value={confirmBeneficiaryAcc}
                  onChange={(e) => {
                    const value = e.currentTarget.value;
                    if (/^\d*$/.test(value)) {
                      setConfirmBeneficiaryAcc(value);
                    }
                  }}
                  onCopy={(e) => e.preventDefault()}
                  onPaste={(e) => e.preventDefault()}
                  onCut={(e) => e.preventDefault()}
                  withAsterisk
                  disabled={isVisibilityLocked}
                />
              </Group>

              <Group align="center" gap="sm">
                <Button variant="filled" color="blue" onClick={handleValidate}>
                  Validate
                </Button>
                {validationStatus === "success" && <Text c="green">{beneficiaryName}</Text>}
                {validationStatus === "error" && <Text c="red">{beneficiaryName}</Text>}
              </Group>

              <Group grow>
                <Select
                  label="Beneficiary A/c Type"
                  placeholder="Select type"
                  data={["Savings", "Current"]}
                  value={beneficiaryType}
                  onChange={setBeneficiaryType}
                  withAsterisk
                />

                <TextInput
                  label="Amount"
                  type="number"
                  value={amount}
                  onChange={(e) => setAmount(e.currentTarget.value)}
                  withAsterisk
                />

                <TextInput
                  label="Remarks"
                  placeholder="Enter remarks"
                  value={remarks}
                  onChange={(e) => setRemarks(e.currentTarget.value)}
                  withAsterisk
                />
              </Group>

              {showTxnPassword && (
                <TextInput
                  label="Transaction Password"
                  placeholder="Enter transaction password"
                  type="password"
                  value={txnPassword}
                  onChange={(e) => setTxnPassword(e.currentTarget.value)}
                  withAsterisk
                />
              )}

              {showOtpField && (
                <TextInput
                  label="OTP"
                  placeholder="Enter OTP"
                  value={otp}
                  onChange={(e) => setOtp(e.currentTarget.value)}
                  withAsterisk
                />
              )}

              <Group justify="flex-start">
                <Button
                  variant="filled"
                  color="blue"
                  onClick={handleProceed}
                  loading={isSubmitting}
                  disabled={validationStatus !== "success"}
                >
                  {showOtpField ? "Proceed to Pay" : "Proceed"}
                </Button>
              </Group>
            </Stack>
          </div>
        </ScrollArea>
      ) : (
        <Text size="lg" mt="md">
          hii
        </Text>
      )}
    </Paper>
  );
}
