import type {
  CoreMemberDetailed,
  VpAccountDetails,
} from "@/lib/features/members/types";
import type { CoreAccountOverview } from "@/lib/features/members/query";
import type { CuratedEventsBooking } from "@/lib/features/bookings/curated/types";
import type { CoreBookingUnit } from "@/lib/features/bookings/types";
import {
  ActionIcon,
  Badge,
  Divider,
  Flex,
  Group,
  Stack,
  Tabs,
  Text,
  Title,
} from "@mantine/core";
import {
  IconArrowLeft,
  IconClipboardText,
  IconFileDescription,
  IconUserOff,
  IconUsers,
} from "@tabler/icons-react";
import { useEffect, useState } from "react";
import moment from "moment";
import { Link, useLocation, useNavigate } from "react-router";
import AccountDetails from "./(widgets)/accountDetails";
import DeleteAccountControl from "./(widgets)/deleteAccountControl";
import AccountDeletionPanel from "./(widgets)/accountDeletionPanel";
import { AccountOverview } from "./(widgets)/accountOverview";
import { MemberPointsControl } from "./(widgets)/memberPoints";
import { accountStatusTone } from "@/routes/members/_components/statusColors";
import MemberContacts from "./(widgets)/memberContacts";
import MembershipBookings from "./(widgets)/membershipBookings";
import type { BookingTabValue } from "./(widgets)/membershipBookings";

const MemberClientPage: React.FC<{
  account: VpAccountDetails;
  contacts: CoreMemberDetailed[];
  bookings?: { data: CoreBookingUnit[]; totalRows: number };
  bookingsByType?: {
    internal: { data: CoreBookingUnit[]; totalRows: number };
    external: { data: CoreBookingUnit[]; totalRows: number };
  };
  curatedBookings?: { data: CuratedEventsBooking[]; totalRows: number };
  bookingEntityType?: BookingTabValue;
  canViewBookings?: boolean;
  canDeleteAccount?: boolean;
  backHref?: string;
  backLabel?: string;
  canViewPoints?: boolean;
  overview?: CoreAccountOverview;
  syncTabWithUrl?: boolean;
  contactHrefBase?: string;
  pointsAllocated?: number | null;
}> = ({
  account,
  contacts,
  bookings,
  bookingsByType,
  curatedBookings,
  bookingEntityType = "internal",
  canViewBookings = false,
  canViewPoints = false,
  canDeleteAccount = false,
  backHref,
  backLabel,
  overview,
  syncTabWithUrl = true,
  contactHrefBase,
  pointsAllocated,
}) => {
  const navigate = useNavigate();
  const { search } = useLocation();
  const canDelete = canDeleteAccount;
  const searchParams = new URLSearchParams(search);
  const paramsTab = searchParams.get("tab") ?? "accountDetails";
  const [selectedTab, setSelectedTab] = useState(
    syncTabWithUrl ? (paramsTab ?? "accountDetails") : "accountDetails",
  );
  useEffect(() => {
    if (syncTabWithUrl) setSelectedTab(paramsTab);
  }, [paramsTab, syncTabWithUrl]);
  const handleTabClick = (value: string) => {
    setSelectedTab(value);
    if (!syncTabWithUrl) return;

    const nextSearchParams = new URLSearchParams(search);
    nextSearchParams.set("tab", value);
    if (value === "bookings") {
      nextSearchParams.set("type", bookingEntityType);
      nextSearchParams.set("page", "1");
      nextSearchParams.set("pageSize", "50");
    } else {
      nextSearchParams.delete("type");
      nextSearchParams.delete("page");
      nextSearchParams.delete("pageSize");
    }
    navigate(`?${nextSearchParams.toString()}`);
  };
  return (
    <Stack gap={"sm"} pb={34}>
      <Group
        justify="space-between"
        align="flex-start"
        wrap="nowrap"
        mb={20}
        pb={20}
        px={28}
        style={{
          borderBottom:
            "1px solid light-dark(var(--mantine-color-gray-2), var(--mantine-color-dark-4))",
        }}
      >
        <Stack gap={"xs"}>
          {backHref ? (
            <Group gap={6}>
              <ActionIcon
                variant="subtle"
                component={Link}
                to={backHref}
                aria-label="Back"
              >
                <IconArrowLeft size={18} />
              </ActionIcon>
              <Text fz={12} c="dimmed">
                Back{backLabel ? ` to ${backLabel}` : ""}
              </Text>
            </Group>
          ) : null}
          <Flex direction={"row"} gap={"xs"} align={"center"}>
            <Title fz={"h3"} fw={500}>
              {account.AccountID}
            </Title>
            <Divider orientation="vertical" h={26} my={"auto"} />
            <Text c="var(--promo-accent)" size="18px" tt={"capitalize"}>
              {account.AccountType?.toLowerCase()}
            </Text>
          </Flex>
          {overview?.lastSession ? (
            <Text fz={12} c="dimmed">
              Last login {moment(overview.lastSession.created_at).fromNow()} ·{" "}
              {overview.lastSession.application_type ?? "unknown app"}
              {overview.lastSession.application_version
                ? ` ${overview.lastSession.application_version}`
                : ""}
              {overview.lastSession.os ? ` · ${overview.lastSession.os}` : ""}
              {overview.lastSession.ip_address
                ? ` · ${overview.lastSession.ip_address}`
                : ""}
              {overview.lastSession.member_number
                ? ` · member ${overview.lastSession.member_number}`
                : ""}
            </Text>
          ) : overview ? (
            <Text fz={12} c="dimmed">
              No app login recorded
            </Text>
          ) : null}
          <Badge
            {...accountStatusTone(account?.AccountStatus)}
            radius={4}
            fz={12}
            tt={"capitalize"}
            size="lg"
            fw={500}
          >
            {account?.AccountStatus?.toLowerCase()}
          </Badge>
        </Stack>

        <MemberPointsControl
          membershipNumber={canViewPoints ? account?.AccountID : undefined}
        />

        {canDelete && (
          <DeleteAccountControl membershipNumber={account?.AccountID} />
        )}
      </Group>

      <AccountOverview overview={overview} />

      <Tabs defaultValue="accountDetails" value={selectedTab} mt="md">
        <Tabs.List px={28}>
          {TAB_HEADS.filter(({ value }) => {
            if (value === "bookings") return canViewBookings;
            if (value === "deletion") return canDelete;
            return true;
          }).map(({ label, value, icon }) => (
            <Tabs.Tab
              key={value}
              fw={500}
              fz={14}
              value={value}
              onClick={() => handleTabClick(value)}
            >
              <Flex align={"center"} gap={3}>
                {icon}
                <Text>{label}</Text>
              </Flex>
            </Tabs.Tab>
          ))}
        </Tabs.List>

        <Tabs.Panel value="accountDetails" pt="md">
          {account ? (
            <AccountDetails
              account={account ?? {}}
              pointsAllocated={pointsAllocated}
            />
          ) : (
            ""
          )}
        </Tabs.Panel>

        <Tabs.Panel value="members" pt="md">
          {contacts ? (
            <MemberContacts
              contacts={contacts}
              contactHrefBase={contactHrefBase}
            />
          ) : (
            ""
          )}
        </Tabs.Panel>

        <Tabs.Panel value="deletion" pt="md">
          {canDelete && account?.AccountID ? (
            <AccountDeletionPanel accountId={account.AccountID} />
          ) : null}
        </Tabs.Panel>

        <Tabs.Panel value="bookings" pt="md">
          {canViewBookings ? (
            <MembershipBookings
              bookings={
                bookingsByType ?? {
                  internal: bookings ?? { data: [], totalRows: 0 },
                  external: { data: [], totalRows: 0 },
                }
              }
              curatedBookings={curatedBookings}
              selectedBookingType={bookingEntityType}
              enableTypeTabs={Boolean(bookingsByType) && syncTabWithUrl}
            />
          ) : null}
        </Tabs.Panel>
      </Tabs>
    </Stack>
  );
};

export default MemberClientPage;

const TAB_HEADS = [
  {
    label: "Account Details",
    value: "accountDetails",
    icon: <IconClipboardText size={20} />,
  },
  {
    label: "Members",
    value: "members",
    icon: <IconUsers size={20} />,
  },
  {
    label: "Bookings",
    value: "bookings",
    icon: <IconFileDescription size={19} />,
  },
  {
    label: "Deletion",
    value: "deletion",
    icon: <IconUserOff size={19} />,
  },
];
