import { useQuery } from "@apollo/client/react";
import { Card, Col, Row } from "antd";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import AlertComponent from "../../components/alert/alert.component";
import CsiResponseFormContainer from "../../components/csi-response-form/csi-response-form.container";
import CsiResponseListPaginated from "../../components/csi-response-list-paginated/csi-response-list-paginated.component";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import { QUERY_CSI_RESPONSE_PAGINATED } from "../../graphql/csi.queries";
import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import { selectBodyshop } from "../../redux/user/user.selectors";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import FeatureWrapperComponent from "../../components/feature-wrapper/feature-wrapper.component";
import UpsellComponent, { upsellEnum } from "../../components/upsell/upsell.component";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop
});

const mapDispatchToProps = (dispatch) => ({
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs)),
  setSelectedHeader: (key) => dispatch(setSelectedHeader(key))
});

export function ShopCsiContainer({ bodyshop, setBreadcrumbs, setSelectedHeader }) {
  const { t } = useTranslation();

  const { loading, error, data, refetch } = useQuery(QUERY_CSI_RESPONSE_PAGINATED, {
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only"
  });

  useEffect(() => {
    document.title = t("titles.shop-csi", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)"
      })
    });
    setSelectedHeader("shop-csi");
    setBreadcrumbs([
      {
        link: "/manage/shop",
        label: t("titles.bc.shop", { shopname: bodyshop.shopname })
      },
      { link: "/manage/shop/csi", label: t("titles.bc.shop-csi") }
    ]);
  }, [t, setBreadcrumbs, bodyshop.shopname, setSelectedHeader]);

  if (error) return <AlertComponent title={error.message} type="error" />;

  return (
    <FeatureWrapperComponent
      featureName="csi"
      noauth={
        <Card>
          <UpsellComponent upsell={upsellEnum().csi.general} />
        </Card>
      }
    >
      <RbacWrapper action="csi:page">
        <Row gutter={16}>
          <Col span={10}>
            <CsiResponseListPaginated
              refetch={refetch}
              loading={loading}
              responses={data ? data.csi : []}
              total={data ? data.csi_aggregate.aggregate.count : 0}
            />
          </Col>
          <Col span={14}>
            <CsiResponseFormContainer />
          </Col>
        </Row>
      </RbacWrapper>
    </FeatureWrapperComponent>
  );
}

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