'use client';

import Link from 'next/link';
import Image from 'next/image';
import { useState, useEffect } from 'react';

export default function Footer() {
  const currentYear = new Date().getFullYear();
  const [theme, setTheme] = useState<'light' | 'dark'>('dark');

  useEffect(() => {
    const stored = localStorage.getItem('theme') as 'light' | 'dark' | null;
    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    setTheme(stored || (prefersDark ? 'dark' : 'light'));
    document.documentElement.classList.toggle('dark', (stored || (prefersDark ? 'dark' : 'light')) === 'dark');
  }, []);

  const footerSections = [
    {
      title: 'Product',
      links: [
        { label: 'Features', href: '/features' },
        { label: 'Pricing', href: '/pricing' },
        { label: 'Documentation', href: '/docs' },
        { label: 'About', href: '/about' },
      ],
    },
    {
      title: 'Resources',
      links: [
        { label: 'Getting Started', href: '/docs/installation' },
        { label: 'API Reference', href: '/docs/api' },
        { label: 'Community', href: '#' },
      ],
    },
    {
      title: 'Support',
      links: [
        { label: 'Contact', href: 'mailto:support@dashcaddy.net' },
        { label: 'Docs', href: '/docs' },
        { label: 'Privacy Policy', href: '#' },
        { label: 'Terms of Service', href: '#' },
      ],
    },
  ];

  const socialLinks = [
    {
      icon: (
        <svg className="h-5 w-5" fill="currentColor" viewBox="0 0 24 24">
          <path d="M20.317 4.37a19.791 19.791 0 00-4.885-1.515a.074.074 0 00-.079.037c-.211.375-.444.864-.607 1.25a18.27 18.27 0 00-5.487 0c-.163-.386-.395-.875-.607-1.25a.077.077 0 00-.079-.037A19.736 19.736 0 003.677 4.37a.07.07 0 00-.032.027C.533 9.046-.32 13.58.099 18.057a.082.082 0 00.031.057 19.9 19.9 0 005.993 3.03.078.078 0 00.084-.028c.462-.63.873-1.295 1.226-1.994a.076.076 0 00-.042-.106 13.107 13.107 0 01-1.872-.892.077.077 0 01-.008-.128 10.2 10.2 0 00.372-.294.075.075 0 01.078-.01c3.928 1.793 8.18 1.793 12.062 0a.075.075 0 01.079.009c.12.098.246.198.373.295a.077.077 0 01-.006.127 12.299 12.299 0 01-1.873.892.076.076 0 00-.041.107c.359.698.77 1.364 1.225 1.994a.077.077 0 00.084.028 19.839 19.839 0 006.002-3.03.076.076 0 00.032-.057c.534-4.506-.9-8.4-3.821-11.865a.055.055 0 00-.032-.027zM8.02 15.33c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.948-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.948 2.419-2.157 2.419zm7.975 0c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.948-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.948 2.419-2.157 2.419z" />
        </svg>
      ),
      label: 'Contact',
      href: 'mailto:support@dashcaddy.net',
    },
  ];

  return (
    <footer className="border-t border-surface-700/50 bg-surface-950">
      <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8 py-12">
        <div className="grid grid-cols-1 md:grid-cols-4 gap-8 mb-8">
          {/* Brand Section */}
          <div>
            <Link href="/" className="flex items-center transition-opacity hover:opacity-80 mb-4">
              <Image
                src={theme === 'dark' ? '/images/brand-wide-light.png' : '/images/brand-wide-dark.png'}
                alt="DashCaddy"
                width={160}
                height={53}
                className="h-9 w-auto"
              />
            </Link>
            <p className="text-surface-400 text-sm leading-relaxed mb-4">
              Self-hosted Docker dashboard with automatic SSL, DNS, and reverse proxy. Making self-hosting beautiful and effortless.
            </p>
            <div className="flex items-center gap-4">
              {socialLinks.map((link: any) => (
                <a
                  key={link.label}
                  href={link.href}
                  className="text-surface-400 hover:text-brand-400 transition-colors"
                  aria-label={link.label}
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  {link.icon}
                </a>
              ))}
            </div>
          </div>

          {/* Link Sections */}
          {footerSections.map((section) => (
            <div key={section.title}>
              <h3 className="text-sm font-semibold text-surface-50 mb-4">
                {section.title}
              </h3>
              <ul className="space-y-3">
                {section.links.map((link) => (
                  <li key={link.label}>
                    <Link
                      href={link.href}
                      className="text-sm text-surface-400 hover:text-brand-400 transition-colors"
                    >
                      {link.label}
                    </Link>
                  </li>
                ))}
              </ul>
            </div>
          ))}
        </div>

        {/* Support Link */}
        <div className="border-t border-surface-700/50 pt-8 mb-8">
          <p className="text-sm text-surface-400">
            Need help? Email us at{' '}
            <a
              href="mailto:support@dashcaddy.net"
              className="text-brand-400 hover:text-brand-300 transition-colors font-medium"
            >
              support@dashcaddy.net
            </a>
          </p>
        </div>

        {/* Copyright with samiahmed7777 logo */}
        <div className="border-t border-surface-700/50 pt-8">
          <div className="flex flex-col sm:flex-row items-center justify-center gap-3">
            <p className="text-sm text-surface-500">
              &copy; {currentYear} DashCaddy. All rights reserved. A product by
            </p>
            <Image
              src="/images/samiahmed7777-logo.png"
              alt="samiahmed7777"
              width={160}
              height={32}
              className="h-7 w-auto opacity-80 hover:opacity-100 transition-opacity"
            />
          </div>
        </div>
      </div>
    </footer>
  );
}
