import React, { useState, useEffect } from "react";
import { apiFetch } from "@/utils/api";
import { Card, CardContent } from "@/components/ui/card";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { DateTime } from "luxon";
import { BusinessRangeSelect } from "@/components/dashboard/shared/BusinessRangeSelect";
import { Button } from "@/components/ui/button";
import { Mail, MessageSquare, BookOpen } from "lucide-react";
import { CARD_STYLES } from "@/lib/dashboard/designTokens";
import {
  DashboardSectionHeader,
  DashboardErrorState,
  DashboardTable,
  TableSkeleton,
} from "@/components/dashboard/shared";

// Helper functions for formatting
const formatRate = (value, isSMS = false, hideForSMS = false) => {
  if (isSMS && hideForSMS) return "N/A";
  if (typeof value !== "number") return "0.0%";
  return `${(value * 100).toFixed(1)}%`;
};

const formatCurrency = (value) => {
  if (typeof value !== "number") return "$0";
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  }).format(value);
};

// MetricCell content component for displaying campaign metrics (returns content, not <td>)
const MetricCellContent = ({
  value,
  count,
  isMonetary = false,
  showConversionRate = false,
  totalRecipients = 0,
  isSMS = false,
  hideForSMS = false,
}) => {
  if (isSMS && hideForSMS) {
    return (
      <div className="text-center">
        <div className="text-muted-foreground text-lg font-semibold">N/A</div>
        <div className="text-muted-foreground text-sm">-</div>
      </div>
    );
  }

  return (
    <div className="text-center">
      <div className="text-blue-600 dark:text-blue-400 text-lg font-semibold">
        {isMonetary ? formatCurrency(value) : formatRate(value, isSMS, hideForSMS)}
      </div>
      <div className="text-muted-foreground text-sm">
        {count?.toLocaleString() || 0} {count === 1 ? "recipient" : "recipients"}
        {showConversionRate &&
          totalRecipients > 0 &&
          ` (${((count / totalRecipients) * 100).toFixed(2)}%)`}
      </div>
    </div>
  );
};

// Campaign name cell with tooltip
const CampaignCell = ({ campaign }) => {
  const isBlog = campaign.name?.includes("_Blog");
  const isSMS = campaign.channel === 'sms';

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger asChild>
          <div className="cursor-default">
            <div className="flex items-center gap-2">
              {isBlog ? (
                <BookOpen className="h-4 w-4 text-muted-foreground flex-shrink-0" />
              ) : isSMS ? (
                <MessageSquare className="h-4 w-4 text-muted-foreground flex-shrink-0" />
              ) : (
                <Mail className="h-4 w-4 text-muted-foreground flex-shrink-0" />
              )}
              <div className="font-medium text-foreground">
                {campaign.name}
              </div>
            </div>
            <div className="text-sm text-muted-foreground truncate max-w-[300px]">
              {campaign.subject}
            </div>
            <div className="text-xs text-muted-foreground">
              {campaign.send_time
                ? DateTime.fromISO(campaign.send_time).toLocaleString(DateTime.DATETIME_MED)
                : "No date"}
            </div>
          </div>
        </TooltipTrigger>
        <TooltipContent
          side="top"
          className="break-words bg-card text-foreground border dark:border-gray-800"
        >
          <p className="font-medium">{campaign.name}</p>
          <p>{campaign.subject}</p>
          <p className="text-xs text-muted-foreground">
            {campaign.send_time
              ? DateTime.fromISO(campaign.send_time).toLocaleString(DateTime.DATETIME_MED)
              : "No date"}
          </p>
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
};

const KlaviyoCampaigns = ({ className }) => {
  const [campaigns, setCampaigns] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);
  const [selectedChannels, setSelectedChannels] = useState({ email: true, sms: true, blog: true });
  const [selectedTimeRange, setSelectedTimeRange] = useState("last7days");
  const [sortConfig, setSortConfig] = useState({
    key: "send_time",
    direction: "desc",
  });

  const handleSort = (key, direction) => {
    setSortConfig({ key, direction });
  };

  const fetchCampaigns = async () => {
    try {
      setIsLoading(true);
      const response = await apiFetch(
        `/api/klaviyo/reporting/campaigns/${selectedTimeRange}`
      );

      if (!response.ok) {
        throw new Error(`Failed to fetch campaigns: ${response.status}`);
      }

      const data = await response.json();
      setCampaigns(data.data || []);
      setError(null);
    } catch (err) {
      console.error("Error fetching campaigns:", err);
      setError(err.message);
    } finally {
      setIsLoading(false);
    }
  };

  useEffect(() => {
    fetchCampaigns();
    const interval = setInterval(fetchCampaigns, 10 * 60 * 1000);
    return () => clearInterval(interval);
  }, [selectedTimeRange]);

  // Sort campaigns
  const sortedCampaigns = [...campaigns].sort((a, b) => {
    const direction = sortConfig.direction === "desc" ? -1 : 1;

    switch (sortConfig.key) {
      case "send_time":
        return direction * (DateTime.fromISO(a.send_time) - DateTime.fromISO(b.send_time));
      case "delivery_rate":
        return direction * ((a.stats.delivery_rate || 0) - (b.stats.delivery_rate || 0));
      case "open_rate":
        return direction * ((a.stats.open_rate || 0) - (b.stats.open_rate || 0));
      case "click_rate":
        return direction * ((a.stats.click_rate || 0) - (b.stats.click_rate || 0));
      case "click_to_open_rate":
        return direction * ((a.stats.click_to_open_rate || 0) - (b.stats.click_to_open_rate || 0));
      case "conversion_value":
        return direction * ((a.stats.conversion_value || 0) - (b.stats.conversion_value || 0));
      default:
        return 0;
    }
  });

  // Filter campaigns by channels
  const filteredCampaigns = sortedCampaigns.filter(
    (campaign) => {
      const isBlog = campaign?.name?.includes("_Blog");
      const channelType = isBlog ? "blog" : campaign?.channel;
      return selectedChannels[channelType];
    }
  );

  // Column definitions for DashboardTable
  const columns = [
    {
      key: "name",
      header: "Campaign",
      sortable: true,
      sortKey: "send_time",
      render: (_, campaign) => <CampaignCell campaign={campaign} />,
    },
    {
      key: "delivery_rate",
      header: "Delivery",
      align: "center",
      sortable: true,
      render: (_, campaign) => (
        <MetricCellContent
          value={campaign.stats.delivery_rate}
          count={campaign.stats.delivered}
          totalRecipients={campaign.stats.recipients}
          isSMS={campaign.channel === 'sms'}
        />
      ),
    },
    {
      key: "open_rate",
      header: "Opens",
      align: "center",
      sortable: true,
      render: (_, campaign) => (
        <MetricCellContent
          value={campaign.stats.open_rate}
          count={campaign.stats.opens_unique}
          totalRecipients={campaign.stats.recipients}
          isSMS={campaign.channel === 'sms'}
          hideForSMS={true}
        />
      ),
    },
    {
      key: "click_rate",
      header: "Clicks",
      align: "center",
      sortable: true,
      render: (_, campaign) => (
        <MetricCellContent
          value={campaign.stats.click_rate}
          count={campaign.stats.clicks_unique}
          totalRecipients={campaign.stats.recipients}
          isSMS={campaign.channel === 'sms'}
        />
      ),
    },
    {
      key: "click_to_open_rate",
      header: "CTR",
      align: "center",
      sortable: true,
      render: (_, campaign) => (
        <MetricCellContent
          value={campaign.stats.click_to_open_rate}
          count={campaign.stats.clicks_unique}
          totalRecipients={campaign.stats.opens_unique}
          isSMS={campaign.channel === 'sms'}
          hideForSMS={true}
        />
      ),
    },
    {
      key: "conversion_value",
      header: "Orders",
      align: "center",
      sortable: true,
      render: (_, campaign) => (
        <MetricCellContent
          value={campaign.stats.conversion_value}
          count={campaign.stats.conversion_uniques}
          isMonetary={true}
          showConversionRate={true}
          totalRecipients={campaign.stats.recipients}
          isSMS={campaign.channel === 'sms'}
        />
      ),
    },
  ];

  if (isLoading) {
    return (
      <Card className={`h-full ${CARD_STYLES.base}`}>
        <DashboardSectionHeader
          title="Klaviyo Campaigns"
          loading={true}
          compact
          actions={<div className="w-[200px]" />}
          timeSelector={<div className="w-[130px]" />}
        />
        <CardContent className="overflow-y-auto pl-4 max-h-[400px] mb-4">
          <TableSkeleton rows={15} columns={6} variant="detailed" />
        </CardContent>
      </Card>
    );
  }

  return (
    <Card className={`h-full ${CARD_STYLES.base}`}>
      {error && (
        <DashboardErrorState
          title="Failed to load campaigns"
          message={error}
          className="mx-6 mt-4"
        />
      )}
      <DashboardSectionHeader
        title="Klaviyo Campaigns"
        compact
        actions={
          <div className="flex gap-1 items-center">
            <Button
              variant={selectedChannels.email ? "default" : "outline"}
              size="sm"
              onClick={() => setSelectedChannels(prev => {
                if (prev.email && Object.values(prev).filter(Boolean).length === 1) {
                  return { email: true, sms: true, blog: true };
                }
                return { email: true, sms: false, blog: false };
              })}
            >
              <Mail className="h-4 w-4" />
              <span className="hidden sm:inline">Email</span>
            </Button>
            <Button
              variant={selectedChannels.sms ? "default" : "outline"}
              size="sm"
              onClick={() => setSelectedChannels(prev => {
                if (prev.sms && Object.values(prev).filter(Boolean).length === 1) {
                  return { email: true, sms: true, blog: true };
                }
                return { email: false, sms: true, blog: false };
              })}
            >
              <MessageSquare className="h-4 w-4" />
              <span className="hidden sm:inline">SMS</span>
            </Button>
            <Button
              variant={selectedChannels.blog ? "default" : "outline"}
              size="sm"
              onClick={() => setSelectedChannels(prev => {
                if (prev.blog && Object.values(prev).filter(Boolean).length === 1) {
                  return { email: true, sms: true, blog: true };
                }
                return { email: false, sms: false, blog: true };
              })}
            >
              <BookOpen className="h-4 w-4" />
              <span className="hidden sm:inline">Blog</span>
            </Button>
          </div>
        }
        timeSelector={
          <BusinessRangeSelect value={selectedTimeRange} onValueChange={setSelectedTimeRange} />
        }
      />
      <CardContent className="pl-4 mb-4">
        <DashboardTable
          columns={columns}
          data={filteredCampaigns}
          getRowKey={(campaign) => campaign.id}
          sortConfig={sortConfig}
          onSort={handleSort}
          maxHeight="md"
          stickyHeader
          bordered
        />
      </CardContent>
    </Card>
  );
};

export default KlaviyoCampaigns;
