"use client";

import { usePathname } from "next/navigation";
import { Bell, Search } from "lucide-react";
import { useAuthStore } from "@/store/authStore";
import { useState } from "react";

const PAGE_TITLES: Record<string, string> = {
  "/": "Dashboard",
  "/gorevler": "Görev Yönetimi",
  "/projeler": "Proje Yönetimi",
  "/crm": "CRM — Müşteri Yönetimi",
  "/finans": "Gelir / Gider Takibi",
  "/notlar": "Not Defteri",
  "/takvim": "Takvim",
  "/hedefler": "Hedef Takibi",
  "/profil": "Profilim",
};

function getPageTitle(pathname: string): string {
  // Tam eşleşme
  if (PAGE_TITLES[pathname]) return PAGE_TITLES[pathname];
  // Prefix eşleşme
  for (const [key, val] of Object.entries(PAGE_TITLES)) {
    if (pathname.startsWith(key) && key !== "/") return val;
  }
  return "KaplanOS";
}

export function Header() {
  const pathname = usePathname();
  const { user } = useAuthStore();
  const [search, setSearch] = useState("");

  const title = getPageTitle(pathname);
  const greeting = getGreeting();

  return (
    <header className="flex items-center gap-4 px-6 py-4 border-b border-surface-500 bg-[#1a1a1d] flex-shrink-0">
      {/* Sayfa başlığı */}
      <div className="flex-1">
        <h1 className="text-base font-semibold text-surface-100">{title}</h1>
        <p className="text-xs text-surface-500 mt-0.5">
          {greeting}, {user?.full_name || user?.username}
        </p>
      </div>

      {/* Global arama */}
      <div className="relative hidden md:block">
        <Search
          size={14}
          className="absolute left-3 top-1/2 -translate-y-1/2 text-surface-500"
        />
        <input
          type="text"
          placeholder="Ara..."
          value={search}
          onChange={(e) => setSearch(e.target.value)}
          className="kaplan-input pl-8 w-52 h-8 text-xs"
        />
      </div>

      {/* Bildirimler */}
      <button className="relative p-2 rounded-lg text-surface-500 hover:text-surface-300 hover:bg-surface-600 transition-colors">
        <Bell size={18} />
        {/* Bildirim sayısı (ileride dinamik) */}
        <span className="absolute top-1.5 right-1.5 w-1.5 h-1.5 bg-red-500 rounded-full" />
      </button>

      {/* Tarih */}
      <div className="hidden lg:block text-right">
        <p className="text-xs font-medium text-surface-300">
          {new Date().toLocaleDateString("tr-TR", {
            day: "numeric",
            month: "long",
            year: "numeric",
          })}
        </p>
        <p className="text-xs text-surface-500">
          {new Date().toLocaleDateString("tr-TR", { weekday: "long" })}
        </p>
      </div>
    </header>
  );
}

function getGreeting(): string {
  const hour = new Date().getHours();
  if (hour < 12) return "Günaydın";
  if (hour < 18) return "İyi günler";
  return "İyi akşamlar";
}
