import Input from "@/components/Low/Input";
import Modal from "@/components/Modal";
import {PERMISSIONS} from "@/constants/userPermissions";
import useExams from "@/hooks/useExams";
import usePackages from "@/hooks/usePackages";
import useUsers from "@/hooks/useUsers";
import {Module} from "@/interfaces";
import {Exam} from "@/interfaces/exam";
import {Package} from "@/interfaces/paypal";
import {Type, User} from "@/interfaces/user";
import useExamStore from "@/stores/examStore";
import {getExamById} from "@/utils/exams";
import {countExercises} from "@/utils/moduleUtils";
import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table";
import axios from "axios";
import clsx from "clsx";
import {capitalize} from "lodash";
import {useRouter} from "next/router";
import {useState} from "react";
import {BsCheck, BsPencil, BsTrash, BsUpload} from "react-icons/bs";
import {toast} from "react-toastify";
import Select from "react-select";
import {CURRENCIES} from "@/resources/paypal";
import Button from "@/components/Low/Button";

const CLASSES: {[key in Module]: string} = {
	reading: "text-ielts-reading",
	listening: "text-ielts-listening",
	speaking: "text-ielts-speaking",
	writing: "text-ielts-writing",
	level: "text-ielts-level",
};

const columnHelper = createColumnHelper<Package>();

type DurationUnit = "days" | "weeks" | "months" | "years";

function PackageCreator({pack, onClose}: {pack?: Package; onClose: () => void}) {
	const [duration, setDuration] = useState(pack?.duration || 1);
	const [unit, setUnit] = useState<DurationUnit>(pack?.duration_unit || "months");

	const [price, setPrice] = useState(pack?.price || 0);
	const [currency, setCurrency] = useState<string>(pack?.currency || "OMR");

	const submit = () => {
		(pack ? axios.patch : axios.post)(pack ? `/api/packages/${pack.id}` : "/api/packages", {
			duration,
			duration_unit: unit,
			price,
			currency,
		})
			.then(() => {
				toast.success("New payment has been created successfully!");
				onClose();
			})
			.catch(() => {
				toast.error("Something went wrong, please try again later!");
			});
	};

	return (
		<div className="flex flex-col gap-8 py-8">
			<div className="flex flex-col gap-3">
				<label className="font-normal text-base text-mti-gray-dim">Price *</label>
				<div className="flex gap-4 items-center">
					<Input defaultValue={price} name="price" type="number" onChange={(e) => setPrice(parseInt(e))} />

					<Select
						className="px-4 col-span-2 py-4 w-full text-sm font-normal placeholder:text-mti-gray-cool disabled:bg-mti-gray-platinum/40 disabled:text-mti-gray-dim disabled:cursor-not-allowed bg-white rounded-full border border-mti-gray-platinum focus:outline-none"
						options={CURRENCIES.map(({label, currency}) => ({value: currency, label}))}
						defaultValue={{value: currency || "EUR", label: CURRENCIES.find((c) => c.currency === currency)?.label || "Euro"}}
						onChange={(value) => setCurrency(value?.value || "EUR")}
						value={{value: currency || "EUR", label: CURRENCIES.find((c) => c.currency === currency)?.label || "Euro"}}
						menuPortalTarget={document?.body}
						styles={{
							menuPortal: (base) => ({...base, zIndex: 9999}),
							control: (styles) => ({
								...styles,
								paddingLeft: "4px",
								border: "none",
								outline: "none",
								":focus": {
									outline: "none",
								},
							}),
							option: (styles, state) => ({
								...styles,
								backgroundColor: state.isFocused ? "#D5D9F0" : state.isSelected ? "#7872BF" : "white",
								color: state.isFocused ? "black" : styles.color,
							}),
						}}
					/>
				</div>
			</div>
			<div className="flex flex-col gap-3">
				<label className="font-normal text-base text-mti-gray-dim">Duration *</label>
				<div className="flex gap-4 items-center">
					<Input defaultValue={duration} name="duration" type="number" onChange={(e) => setDuration(parseInt(e))} />
					<Select
						className="px-4 col-span-2 py-4 w-full text-sm font-normal placeholder:text-mti-gray-cool disabled:bg-mti-gray-platinum/40 disabled:text-mti-gray-dim disabled:cursor-not-allowed bg-white rounded-full border border-mti-gray-platinum focus:outline-none"
						options={[
							{value: "days", label: "Days"},
							{value: "weeks", label: "Weeks"},
							{value: "months", label: "Months"},
							{value: "years", label: "Years"},
						]}
						defaultValue={{value: "months", label: "Months"}}
						onChange={(value) => setUnit((value?.value as DurationUnit) || "months")}
						value={{value: unit, label: capitalize(unit)}}
						menuPortalTarget={document?.body}
						styles={{
							menuPortal: (base) => ({...base, zIndex: 9999}),
							control: (styles) => ({
								...styles,
								paddingLeft: "4px",
								border: "none",
								outline: "none",
								":focus": {
									outline: "none",
								},
							}),
							option: (styles, state) => ({
								...styles,
								backgroundColor: state.isFocused ? "#D5D9F0" : state.isSelected ? "#7872BF" : "white",
								color: state.isFocused ? "black" : styles.color,
							}),
						}}
					/>
				</div>
			</div>
			<div className="flex w-full justify-end items-center gap-8 mt-8">
				<Button variant="outline" color="red" className="w-full max-w-[200px]" onClick={onClose}>
					Cancel
				</Button>
				<Button className="w-full max-w-[200px]" onClick={submit} disabled={!duration || !price}>
					Submit
				</Button>
			</div>
		</div>
	);
}

export default function PackageList({user}: {user: User}) {
	const [isCreating, setIsCreating] = useState(false);
	const [editingPackage, setEditingPackage] = useState<Package>();

	const {packages, reload} = usePackages();

	const deletePackage = async (pack: Package) => {
		if (!confirm(`Are you sure you want to delete this package?`)) return;

		axios
			.delete(`/api/packages/${pack.id}`)
			.then(() => toast.success(`Deleted the "${pack.id}" exam`))
			.catch((reason) => {
				if (reason.response.status === 404) {
					toast.error("Package not found!");
					return;
				}

				if (reason.response.status === 403) {
					toast.error("You do not have permission to delete this exam!");
					return;
				}

				toast.error("Something went wrong, please try again later.");
			})
			.finally(reload);
	};

	const defaultColumns = [
		columnHelper.accessor("id", {
			header: "ID",
			cell: (info) => info.getValue(),
		}),
		columnHelper.accessor("duration", {
			header: "Duration",
			cell: (info) => (
				<span>
					{info.getValue()} {info.row.original.duration_unit}
				</span>
			),
		}),
		columnHelper.accessor("price", {
			header: "Price",
			cell: (info) => (
				<span>
					{info.getValue()} {info.row.original.currency}
				</span>
			),
		}),
		{
			header: "",
			id: "actions",
			cell: ({row}: {row: {original: Package}}) => {
				return (
					<div className="flex gap-4">
						{["developer", "admin"].includes(user.type) && (
							<div data-tip="Edit" className="cursor-pointer tooltip" onClick={() => setEditingPackage(row.original)}>
								<BsPencil className="hover:text-mti-purple-light transition ease-in-out duration-300" />
							</div>
						)}
						{["developer", "admin"].includes(user.type) && (
							<div data-tip="Delete" className="cursor-pointer tooltip" onClick={() => deletePackage(row.original)}>
								<BsTrash className="hover:text-mti-purple-light transition ease-in-out duration-300" />
							</div>
						)}
					</div>
				);
			},
		},
	];

	const table = useReactTable({
		data: packages,
		columns: defaultColumns,
		getCoreRowModel: getCoreRowModel(),
	});

	const closeModal = () => {
		setIsCreating(false);
		setEditingPackage(undefined);
		reload();
	};

	return (
		<div className="w-full h-full rounded-xl">
			<Modal
				isOpen={isCreating || !!editingPackage}
				onClose={closeModal}
				title={editingPackage ? `Editing ${editingPackage.id}` : "New Package"}>
				<PackageCreator onClose={closeModal} pack={editingPackage} />
			</Modal>
			<table className="bg-mti-purple-ultralight/40 w-full">
				<thead>
					{table.getHeaderGroups().map((headerGroup) => (
						<tr key={headerGroup.id}>
							{headerGroup.headers.map((header) => (
								<th className="p-4 text-left" key={header.id}>
									{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
								</th>
							))}
						</tr>
					))}
				</thead>
				<tbody className="px-2">
					{table.getRowModel().rows.map((row) => (
						<tr className="odd:bg-white even:bg-mti-purple-ultralight/40 rounded-lg py-2" key={row.id}>
							{row.getVisibleCells().map((cell) => (
								<td className="px-4 py-2" key={cell.id}>
									{flexRender(cell.column.columnDef.cell, cell.getContext())}
								</td>
							))}
						</tr>
					))}
				</tbody>
			</table>
			<button
				onClick={() => setIsCreating(true)}
				className="w-full py-2 bg-mti-purple-light hover:bg-mti-purple transition ease-in-out duration-300 text-white">
				New Package
			</button>
		</div>
	);
}
