"use client";

import { forwardRef } from "react";
import {
  ServerIcon,
  CalendarIcon,
  CurrencyYenIcon,
  IdentificationIcon,
  TagIcon,
} from "@heroicons/react/24/outline";
import { StatusPill } from "@/components/atoms/status-pill";
import { SubCard } from "@/components/molecules/SubCard/SubCard";
import type { Subscription } from "@customer-portal/domain/subscriptions";

import { useFormatCurrency } from "@/shared/hooks";
import { cn, formatIsoDate } from "@/shared/utils";
import {
  getBillingCycleLabel,
  getSubscriptionStatusIcon,
  getSubscriptionStatusVariant,
} from "@/features/subscriptions/utils/status-presenters";

interface SubscriptionDetailsProps {
  subscription: Subscription;
  showServiceSpecificSections?: boolean;
  className?: string;
}

const formatDate = (dateString: string | undefined) => {
  return formatIsoDate(dateString);
};

const isSimService = (productName: string) => {
  return productName.toLowerCase().includes("sim");
};

const isInternetService = (productName: string) => {
  const name = productName.toLowerCase();
  return name.includes("internet") || name.includes("broadband") || name.includes("fiber");
};

const isVpnService = (productName: string) => {
  return productName.toLowerCase().includes("vpn");
};

export const SubscriptionDetails = forwardRef<HTMLDivElement, SubscriptionDetailsProps>(
  ({ subscription, showServiceSpecificSections = true, className }, ref) => {
    const { formatCurrency } = useFormatCurrency();

    return (
      <div ref={ref} className={cn("space-y-6", className)}>
        {/* Main Details Card */}
        <SubCard
          header={
            <div className="flex items-center justify-between">
              <div className="flex items-center space-x-3">
                {getSubscriptionStatusIcon(subscription.status)}
                <div>
                  <h3 className="text-lg font-semibold text-foreground">Subscription Details</h3>
                  <p className="text-sm text-muted-foreground">Service subscription information</p>
                </div>
              </div>
              <StatusPill
                label={subscription.status}
                variant={getSubscriptionStatusVariant(subscription.status)}
                size="lg"
              />
            </div>
          }
        >
          <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
            {/* Billing Amount */}
            <div className="space-y-2">
              <div className="flex items-center space-x-2">
                <CurrencyYenIcon className="h-5 w-5 text-muted-foreground" />
                <h4 className="text-sm font-medium text-muted-foreground uppercase tracking-wider">
                  Billing Amount
                </h4>
              </div>
              <p className="text-2xl font-bold text-foreground">
                {formatCurrency(subscription.amount)}
              </p>
              <p className="text-sm text-muted-foreground">
                {getBillingCycleLabel(subscription.cycle)}
              </p>
            </div>

            {/* Next Due Date */}
            <div className="space-y-2">
              <div className="flex items-center space-x-2">
                <CalendarIcon className="h-5 w-5 text-muted-foreground" />
                <h4 className="text-sm font-medium text-muted-foreground uppercase tracking-wider">
                  Next Due Date
                </h4>
              </div>
              <p className="text-lg font-semibold text-foreground">
                {formatDate(subscription.nextDue)}
              </p>
              <p className="text-sm text-muted-foreground">Due date</p>
            </div>

            {/* Registration Date */}
            <div className="space-y-2">
              <div className="flex items-center space-x-2">
                <TagIcon className="h-5 w-5 text-muted-foreground" />
                <h4 className="text-sm font-medium text-muted-foreground uppercase tracking-wider">
                  Registration Date
                </h4>
              </div>
              <p className="text-lg font-semibold text-foreground">
                {formatDate(subscription.registrationDate)}
              </p>
              <p className="text-sm text-muted-foreground">Service created</p>
            </div>
          </div>

          {/* Additional Details */}
          <div className="mt-6 pt-6 border-t border-border">
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
              <div className="space-y-2">
                <div className="flex items-center space-x-2">
                  <IdentificationIcon className="h-5 w-5 text-muted-foreground" />
                  <h4 className="text-sm font-medium text-muted-foreground">Service ID</h4>
                </div>
                <p className="text-base font-medium text-foreground">{subscription.serviceId}</p>
              </div>

              {subscription.orderNumber && (
                <div className="space-y-2">
                  <div className="flex items-center space-x-2">
                    <TagIcon className="h-5 w-5 text-muted-foreground" />
                    <h4 className="text-sm font-medium text-muted-foreground">Order Number</h4>
                  </div>
                  <p className="text-base font-medium text-foreground">
                    {subscription.orderNumber}
                  </p>
                </div>
              )}

              {subscription.groupName && (
                <div className="space-y-2">
                  <h4 className="text-sm font-medium text-muted-foreground">Product Group</h4>
                  <p className="text-base font-medium text-foreground">{subscription.groupName}</p>
                </div>
              )}

              {subscription.paymentMethod && (
                <div className="space-y-2">
                  <h4 className="text-sm font-medium text-muted-foreground">Payment Method</h4>
                  <p className="text-base font-medium text-foreground">
                    {subscription.paymentMethod}
                  </p>
                </div>
              )}
            </div>
          </div>

          {/* Custom Fields */}
          {subscription.customFields && Object.keys(subscription.customFields).length > 0 && (
            <div className="mt-6 pt-6 border-t border-border">
              <h4 className="text-sm font-medium text-muted-foreground mb-3">
                Additional Information
              </h4>
              <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                {Object.entries(subscription.customFields).map(([key, value]) => (
                  <div key={key} className="space-y-1">
                    <p className="text-sm text-muted-foreground capitalize">
                      {key.replace(/([A-Z])/g, " $1").trim()}
                    </p>
                    <p className="text-sm font-medium text-foreground">{value}</p>
                  </div>
                ))}
              </div>
            </div>
          )}

          {/* Notes */}
          {subscription.notes && (
            <div className="mt-6 pt-6 border-t border-border">
              <h4 className="text-sm font-medium text-muted-foreground mb-2">Notes</h4>
              <p className="text-sm text-foreground bg-muted rounded-md p-3">
                {subscription.notes}
              </p>
            </div>
          )}
        </SubCard>

        {/* Service-Specific Information */}
        {showServiceSpecificSections && (
          <>
            {/* SIM Service Information */}
            {isSimService(subscription.productName) && (
              <SubCard title="SIM Service Information" icon={<ServerIcon className="h-5 w-5" />}>
                <div className="bg-info-bg border border-info-border rounded-lg p-4">
                  <div className="flex items-start space-x-3">
                    <div className="bg-info/20 rounded-lg p-2">
                      <ServerIcon className="h-5 w-5 text-info" />
                    </div>
                    <div>
                      <h4 className="text-sm font-semibold text-info mb-2">
                        SIM Management Available
                      </h4>
                      <p className="text-sm text-info/80 mb-3">
                        This subscription includes SIM management features such as data usage
                        monitoring, top-up options, and service configuration.
                      </p>
                      <ul className="text-sm text-info/70 space-y-1">
                        <li>• Real-time data usage tracking</li>
                        <li>• Data top-up and plan changes</li>
                        <li>• Service feature toggles</li>
                        <li>• SIM reissue options</li>
                      </ul>
                    </div>
                  </div>
                </div>
              </SubCard>
            )}

            {/* Internet Service Information */}
            {isInternetService(subscription.productName) && (
              <SubCard
                title="Internet Service Information"
                icon={<ServerIcon className="h-5 w-5" />}
              >
                <div className="bg-success-bg border border-success-border rounded-lg p-4">
                  <div className="flex items-start space-x-3">
                    <div className="bg-success/20 rounded-lg p-2">
                      <ServerIcon className="h-5 w-5 text-success" />
                    </div>
                    <div>
                      <h4 className="text-sm font-semibold text-success mb-2">
                        Internet Service Features
                      </h4>
                      <p className="text-sm text-success/80 mb-3">
                        Your internet service includes high-speed connectivity and support options.
                      </p>
                      <ul className="text-sm text-success/70 space-y-1">
                        <li>• High-speed internet connection</li>
                        <li>• 24/7 technical support</li>
                        <li>• Service monitoring</li>
                        <li>• Installation support</li>
                      </ul>
                    </div>
                  </div>
                </div>
              </SubCard>
            )}

            {/* VPN Service Information */}
            {isVpnService(subscription.productName) && (
              <SubCard title="VPN Service Information" icon={<ServerIcon className="h-5 w-5" />}>
                <div className="bg-info-bg border border-info-border rounded-lg p-4">
                  <div className="flex items-start space-x-3">
                    <div className="bg-info/20 rounded-lg p-2">
                      <ServerIcon className="h-5 w-5 text-info" />
                    </div>
                    <div>
                      <h4 className="text-sm font-semibold text-info mb-2">VPN Service Features</h4>
                      <p className="text-sm text-info/80 mb-3">
                        Your VPN service provides secure and private internet access.
                      </p>
                      <ul className="text-sm text-info/70 space-y-1">
                        <li>• Encrypted internet connection</li>
                        <li>• Multiple server locations</li>
                        <li>• No-logs policy</li>
                        <li>• Multi-device support</li>
                      </ul>
                    </div>
                  </div>
                </div>
              </SubCard>
            )}
          </>
        )}
      </div>
    );
  }
);

SubscriptionDetails.displayName = "SubscriptionDetails";
