"use client";

import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
import ReactLenis from "lenis/react";
import ContactSplitForm from "@/components/sections/contact/ContactSplitForm";
import NavbarLayoutFloatingInline from "@/components/navbar/NavbarLayoutFloatingInline";

export default function CalculatorPage() {
  const handleCalculate = (data: Record<string, string>) => {
    // Extract numerical values from the form data
    const avgCallsPerDay = parseFloat(data.avgCallsPerDay || '0');
    const avgConversionRate = parseFloat(data.avgConversionRate || '0') / 100; // Convert percentage to decimal
    const avgValuePerCustomer = parseFloat(data.avgValuePerCustomer || '0');
    const currentAnswerRate = parseFloat(data.currentAnswerRate || '0') / 100; // Convert percentage to decimal

    if (isNaN(avgCallsPerDay) || isNaN(avgConversionRate) || isNaN(avgValuePerCustomer) || isNaN(currentAnswerRate)) {
      alert("Please enter valid numbers for all fields.");
      return;
    }

    // Assume maximum potential answer rate is 100%
    const potentialMaxAnswerRate = 1; 

    // Calculate missed calls per day
    const missedCallsPerDay = avgCallsPerDay * (potentialMaxAnswerRate - currentAnswerRate);

    // Calculate potential additional conversions per day
    const additionalConversionsPerDay = missedCallsPerDay * avgConversionRate;

    // Calculate potential additional revenue per day
    const additionalRevenuePerDay = additionalConversionsPerDay * avgValuePerCustomer;

    // Calculate potential annual revenue increase (assuming 365 days a year)
    const annualRevenueIncrease = additionalRevenuePerDay * 365;

    alert(
      `Based on your inputs:\n` +
      `  Average Calls Per Day: ${avgCallsPerDay}\n` +
      `  Average Conversion Rate: ${avgConversionRate * 100}%\n` +
      `  Average Value Per Customer: $${avgValuePerCustomer.toFixed(2)}\n` +
      `  Current Call Answer Rate: ${currentAnswerRate * 100}%\n\n` +
      `Potential Missed Calls Per Day: ${missedCallsPerDay.toFixed(0)}\n` +
      `Potential Additional Annual Revenue: $${annualRevenueIncrease.toFixed(2)}\n\n` +
      `This is a rough estimate. elevai8 can help you significantly reduce missed calls and recover this lost revenue!`
    );
  };

  return (
    <ThemeProvider
      defaultButtonVariant="elastic-effect"
      defaultTextAnimation="reveal-blur"
      borderRadius="soft"
      contentWidth="small"
      sizing="largeSmallSizeLargeTitles"
      background="noise"
      cardStyle="subtle-shadow"
      primaryButtonStyle="flat"
      secondaryButtonStyle="layered"
      headingFontWeight="bold"
    >
      <ReactLenis root>
        <div id="nav" data-section="nav">
          <NavbarLayoutFloatingInline
            navItems={[
              { name: "Home", id: "/" },
              { name: "Solutions", id: "/#solutions" },
              { name: "Impact", id: "/#impact" },
              { name: "Calculator", id: "/calculator" },
              { name: "Pricing", id: "/#pricing" },
              { name: "Testimonials", id: "/#testimonials" },
              { name: "Contact", id: "/#contact" }
            ]}
            logoSrc="asset://1_1709403332503_group-19-1-svg"
            logoAlt="elevai8 Logo"
            brandName="elevai8"
            button={{ text: "Get Free Demo", href: "/#contact" }}
            animateOnLoad={true}
          />
        </div>

        <div id="calculator" data-section="calculator">
          <ContactSplitForm
            useInvertedBackground={true}
            mediaAnimation="slide-up"
            mediaPosition="left"
            imageSrc="http://img.b2bpic.net/free-photo/financial-calculation-business-concept_23-2149150036.jpg" 
            imageAlt="Calculator and financial data"
            title="Discover Your Hidden Revenue Loss"
            description="Input a few details about your business to estimate the revenue you might be losing due to unanswered calls and how much you could recover with elevai8."
            inputs={[
              { name: "avgCallsPerDay", type: "number", placeholder: "Avg. Calls Per Day", required: true },
              { name: "avgConversionRate", type: "number", placeholder: "Avg. Conversion Rate (%)", required: true },
              { name: "avgValuePerCustomer", type: "number", placeholder: "Avg. Value Per Customer ($)", required: true },
              { name: "currentAnswerRate", type: "number", placeholder: "Current Call Answer Rate (%)", required: true }
            ]}
            buttonText="Calculate My Annual Loss"
            onSubmit={handleCalculate}
          />
        </div>
      </ReactLenis>
    </ThemeProvider>
  );
}