import Layout from "@/components/High/Layout";
import useUser from "@/hooks/useUser";
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
import { sessionOptions } from "@/lib/session";
import { redirect } from "@/utils";
import { requestUser } from "@/utils/api";
import { shouldRedirectHome } from "@/utils/navigation.disabled";
import { withIronSessionSsr } from "iron-session/next";
import Head from "next/head";
import Link from "next/link";
import { BsChevronLeft } from "react-icons/bs";
import { ToastContainer } from "react-toastify";


import approvalWorkflowsData from '../../demo/approval_workflows.json'; // to test locally

import RequestedBy from "@/components/ApprovalWorkflows/RequestedBy";
import StartedOn from "@/components/ApprovalWorkflows/StartedOn";
import Status from "@/components/ApprovalWorkflows/Status";
import WorkflowStepComponent from "@/components/ApprovalWorkflows/WorkflowStepComponent";
import { useState } from "react";

export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }) => {
    const user = await requestUser(req, res);
    if (!user) return redirect("/login")

    if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type))
        return redirect("/")

    const { id } = params as { id: string };

    const workflow = approvalWorkflowsData.find(workflow => workflow.id === id);       // await getApprovalWorkflow(id);

    if (!workflow)
        return redirect("/approval-workflows")

    return {
        props: { user, workflow },
    };
}, sessionOptions);

export default function Home({ workflow }: { workflow: ApprovalWorkflow }) {
    const { user } = useUser({ redirectTo: "/login" });

    const steps = workflow.steps;

    const [selectedIndex, setSelectedIndex] = useState(steps.length - 1);

    const handleStepClick = (index: number) => {
        setSelectedIndex(index);
    };

    return (
        <>
            <Head>
                <title> {workflow.name} | EnCoach</title>
                <meta
                    name="description"
                    content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
                />
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <ToastContainer />
            {user && (
                <Layout user={user} className="gap-6">
                    <section className="flex flex-col gap-0">
                        <div className="flex items-center gap-2">
                            <Link
                                href="/approval-workflows"
                                className="text-mti-purple hover:text-mti-purple-dark transition ease-in-out duration-300 text-xl">
                                <BsChevronLeft />
                            </Link>
                            <h1 className="text-2xl font-semibold">{workflow.name}</h1>
                        </div>
                    </section>
                    <section className="flex flex-col gap-6">
                        <div className="flex flex-row gap-6">
                            <RequestedBy
                                name="Prof. Álvaro Dória"
                                profileImage="/blue-stock-photo.png"
                            />
                            <StartedOn
                                date="15/12/2024"
                            />
                            <Status
                                status="pending"
                            />
                        </div>
                    </section>
                    <section className="flex flex-col gap-0">
                        {steps.map((step, index) => (
                            <WorkflowStepComponent
                                key={index}
                                completed={step.completed}
                                completedBy={step.completedBy}
                                stepNumber={step.stepNumber}
                                stepType={step.stepType}
                                assignees={step.assignees}
                                assigneesType={step.assigneesType}
                                finalStep={index === steps.length - 1}
                                currentStep={steps.findIndex(step => !step.completed) === index}
                                selected={index === selectedIndex} 
                                onClick={() => handleStepClick(index)} 
                            />
                        ))}
                    </section>
                </Layout>
            )}
        </>
    );
}
