import { Icon } from "@iconify/react";
import { useNavigate } from "react-router-dom";
import styled from "styled-components";
import { useAuthStore } from "../store/AuthStore";

export function AccessDenied({ modulo, reason = "plan" }) {
  const navigate = useNavigate();
  const { suscripcion } = useAuthStore();

  const isPlanDenied = reason === "plan";

  return (
    <Container>
      <Card>
        <IconCircle $permission={!isPlanDenied}>
          <Icon
            icon={isPlanDenied ? "lucide:lock" : "lucide:shield-off"}
            width={48}
            height={48}
          />
        </IconCircle>
        <Title>
          {isPlanDenied ? "Módulo no disponible" : "Acceso restringido"}
        </Title>
        <Description>
          {isPlanDenied ? (
            modulo ? (
              <>
                El módulo <strong>{modulo}</strong> no está incluido en tu plan
                actual
                {suscripcion?.plan?.descripcion && (
                  <>
                    {" "}
                    (<strong>{suscripcion.plan.descripcion}</strong>)
                  </>
                )}
                .
              </>
            ) : (
              "No tienes acceso a este recurso con tu plan actual."
            )
          ) : (
            <>
              No tienes permisos para acceder al módulo{" "}
              <strong>{modulo}</strong>. Tu perfil de usuario no incluye los
              privilegios necesarios.
            </>
          )}
        </Description>
        <Hint>
          {isPlanDenied
            ? "Contacta al administrador para actualizar tu suscripción y desbloquear esta funcionalidad."
            : "Contacta al administrador de tu empresa para que te asigne los permisos necesarios."}
        </Hint>
        <Actions>
          <BackButton onClick={() => navigate(-1)}>
            <Icon icon="lucide:arrow-left" />
            Volver
          </BackButton>
          <HomeButton onClick={() => navigate("/")}>
            <Icon icon="lucide:home" />
            Ir al inicio
          </HomeButton>
        </Actions>
      </Card>
    </Container>
  );
}

const Container = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
  padding: 2rem;
`;

const Card = styled.div`
  background: ${({ theme }) => theme.bg};
  border: 1px solid ${({ theme }) => theme.bg4};
  border-radius: 16px;
  padding: 3rem 2.5rem;
  max-width: 480px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
`;

const IconCircle = styled.div`
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: ${({ $permission }) =>
    $permission
      ? "linear-gradient(135deg, #6366f120 0%, #8b5cf620 100%)"
      : "linear-gradient(135deg, #f8853320 0%, #ff6b6b20 100%)"};
  display: flex;
  align-items: center;
  justify-content: center;
  color: ${({ $permission }) => ($permission ? "#6366f1" : "#f88533")};
  margin-bottom: 0.5rem;
`;

const Title = styled.h2`
  font-size: 1.5rem;
  font-weight: 700;
  color: ${({ theme }) => theme.text};
  margin: 0;
`;

const Description = styled.p`
  font-size: 1rem;
  color: ${({ theme }) => theme.text}cc;
  margin: 0;
  line-height: 1.6;

  strong {
    color: #f88533;
  }
`;

const Hint = styled.p`
  font-size: 0.85rem;
  color: ${({ theme }) => theme.text}80;
  margin: 0;
  line-height: 1.5;
`;

const Actions = styled.div`
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
`;

const BackButton = styled.button`
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 10px;
  border: 1px solid ${({ theme }) => theme.bg4};
  background: transparent;
  color: ${({ theme }) => theme.text};
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: ${({ theme }) => theme.bg3};
  }
`;

const HomeButton = styled.button`
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 10px;
  border: none;
  background: linear-gradient(135deg, #f88533 0%, #ff6b6b 100%);
  color: white;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    opacity: 0.9;
    transform: translateY(-1px);
  }
`;
