import { DownCircleFilled } from "@ant-design/icons";
import { useMutation } from "@apollo/client/react";
import { Button, Dropdown } from "antd";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
import { UPDATE_JOB_STATUS } from "../../graphql/jobs.queries";
import { insertAuditTrail } from "../../redux/application/application.actions";
import { selectIsPartsEntry, selectJobReadOnly } from "../../redux/application/application.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors";
import AuditTrailMapping from "../../utils/AuditTrailMappings";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
  jobRO: selectJobReadOnly,
  isPartsEntry: selectIsPartsEntry
});
const mapDispatchToProps = (dispatch) => ({
  insertAuditTrail: ({ jobid, operation, type }) => dispatch(insertAuditTrail({ jobid, operation, type }))
});

export function JobsChangeStatus({ job, bodyshop, jobRO, insertAuditTrail, isPartsEntry }) {
  const { t } = useTranslation();

  const [availableStatuses, setAvailableStatuses] = useState([]);
  const [mutationUpdateJobstatus] = useMutation(UPDATE_JOB_STATUS);
  const notification = useNotification();

  const updateJobStatus = (status) => {
    mutationUpdateJobstatus({
      variables: { jobId: job.id, status: status }
    })
      .then(() => {
        notification.success({ title: t("jobs.successes.save") });
        insertAuditTrail({
          jobid: job.id,
          operation: AuditTrailMapping.jobstatuschange(status),
          type: "jobstatuschange"
        });
        // refetch();
      })
      .catch(() => {
        notification.error({ title: t("jobs.errors.saving") });
      });
  };

  // Updates available statuses based on job and bodyshop context
  useEffect(() => {
    if (!job || !bodyshop) return;

    const { md_ro_statuses } = bodyshop;
    const {
      parts_statuses,
      pre_production_statuses,
      production_statuses,
      post_production_statuses,
      statuses,
      default_invoiced,
      default_exported
    } = md_ro_statuses;

    if (isPartsEntry) {
      // Set parts-specific statuses for parts entry scenario
      setAvailableStatuses(parts_statuses);
      return;
    }

    // Handle non-parts entry scenarios based on job status
    if (pre_production_statuses.includes(job.status)) {
      setAvailableStatuses(pre_production_statuses);
    } else if (production_statuses.includes(job.status)) {
      setAvailableStatuses(production_statuses);
    } else if (post_production_statuses.includes(job.status)) {
      // Filter out invoiced and exported statuses for post-production
      setAvailableStatuses(
        post_production_statuses.filter((status) => status !== default_invoiced && status !== default_exported)
      );
    } else {
      // Default to all statuses if no specific restrictions apply
      console.log("Status didn't match any restrictions. Allowing all status changes.");
      setAvailableStatuses(statuses);
    }
  }, [job, bodyshop, isPartsEntry, setAvailableStatuses]);

  const statusMenu = {
    items: [
      ...(availableStatuses?.map((item) => ({
        key: item,
        label: item
      })) ?? [])
    ],
    onClick: (e) => updateJobStatus(e.key)
  };

  return (
    <Dropdown menu={statusMenu} trigger={["click"]} key="changestatus" disabled={jobRO || !job.converted}>
      <Button shape="round" icon={<DownCircleFilled />} iconPlacement="end">
        <span>{job.status}</span>
      </Button>
    </Dropdown>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(JobsChangeStatus);
