"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Store, Link as LinkIcon, RefreshCw, CheckCircle2 } from "lucide-react";
import { useRouter } from "next/navigation";

export default function ShopifyIntegrationPage() {
  const router = useRouter();
  const [isConnected, setIsConnected] = useState(false);
  const [shopName, setShopName] = useState("");
  const [isConnecting, setIsConnecting] = useState(false);

  const handleConnect = (e: React.FormEvent) => {
    e.preventDefault();
    if (!shopName) return;
    
    setIsConnecting(true);
    // In a real app, this would redirect to your API route to start OAuth:
    // window.location.href = `/api/shopify/auth?shop=${shopName}.myshopify.com`;
    
    // For demonstration, we'll simulate a successful connection after 2 seconds
    setTimeout(() => {
      setIsConnecting(false);
      setIsConnected(true);
    }, 2000);
  };

  return (
    <div className="flex h-full flex-col">
      <div className="flex items-center justify-between border-b border-border bg-card px-6 py-4">
        <div>
          <h1 className="text-xl font-semibold text-foreground">Shopify Integration</h1>
          <p className="text-sm text-muted-foreground">
            Connect your Shopify store to automatically sync orders, fulfillments, and abandoned checkouts.
          </p>
        </div>
        <div>
          {isConnected ? (
            <Button variant="outline" className="gap-2 text-green-600">
              <CheckCircle2 className="h-4 w-4" />
              Connected
            </Button>
          ) : (
            <Button onClick={() => document.getElementById('shop-input')?.focus()} className="gap-2">
              <LinkIcon className="h-4 w-4" />
              Connect to Shopify
            </Button>
          )}
        </div>
      </div>

      <div className="flex-1 overflow-auto p-6">
        {isConnected ? (
          <div className="grid gap-6 md:grid-cols-3">
            <div className="rounded-xl border border-border bg-card p-6 shadow-sm">
              <div className="flex items-center gap-4">
                <div className="flex h-12 w-12 items-center justify-center rounded-lg bg-indigo-50 text-indigo-600">
                  <RefreshCw className="h-6 w-6" />
                </div>
                <div>
                  <h3 className="font-semibold text-foreground">Orders Synced</h3>
                  <p className="text-2xl font-bold text-foreground">1,234</p>
                </div>
              </div>
            </div>
            <div className="rounded-xl border border-border bg-card p-6 shadow-sm">
              <div className="flex items-center gap-4">
                <div className="flex h-12 w-12 items-center justify-center rounded-lg bg-indigo-50 text-indigo-600">
                  <RefreshCw className="h-6 w-6" />
                </div>
                <div>
                  <h3 className="font-semibold text-foreground">Abandoned Checkouts</h3>
                  <p className="text-2xl font-bold text-foreground">56</p>
                </div>
              </div>
            </div>
            <div className="rounded-xl border border-border bg-card p-6 shadow-sm">
              <div className="flex items-center gap-4">
                <div className="flex h-12 w-12 items-center justify-center rounded-lg bg-indigo-50 text-indigo-600">
                  <RefreshCw className="h-6 w-6" />
                </div>
                <div>
                  <h3 className="font-semibold text-foreground">Fulfillments</h3>
                  <p className="text-2xl font-bold text-foreground">892</p>
                </div>
              </div>
            </div>
          </div>
        ) : (
          <div className="flex h-full flex-col items-center justify-center text-center">
            <div className="mb-6 flex h-20 w-20 items-center justify-center rounded-full bg-indigo-50">
              <Store className="h-10 w-10 text-indigo-600" />
            </div>
            <h2 className="mb-2 text-2xl font-bold text-foreground">Connect Your Store</h2>
            <p className="mb-6 max-w-md text-muted-foreground">
              Enter your Shopify store prefix to initiate the connection. You will be redirected to Shopify to authorize the app.
            </p>
            
            <form onSubmit={handleConnect} className="flex w-full max-w-sm flex-col gap-4">
              <div className="flex items-center gap-2">
                <Input 
                  id="shop-input"
                  placeholder="your-store-name" 
                  value={shopName}
                  onChange={(e) => setShopName(e.target.value)}
                  disabled={isConnecting}
                  required
                />
                <span className="text-sm text-muted-foreground">.myshopify.com</span>
              </div>
              <Button type="submit" disabled={isConnecting} className="w-full">
                {isConnecting ? (
                  <>
                    <RefreshCw className="mr-2 h-4 w-4 animate-spin" />
                    Connecting...
                  </>
                ) : (
                  "Login to Shopify"
                )}
              </Button>
            </form>
          </div>
        )}
      </div>
    </div>
  );
}
