import { useState } from "react";
import { investorChats, investorMessages } from "@/mocks/dshnInvestor";
import { useWeb } from "@/pages/web/WebContext";
import { Container, WCard, WBtn, StatusPill, cn } from "@/pages/web/WebUI";
import WebPageHeader from "@/pages/web/components/WebPageHeader";

export default function InvChats() {
  const { params, navigate } = useWeb();
  const [activeId, setActiveId] = useState(params.chatId ?? investorChats[0].id);
  const [input, setInput] = useState("");
  const active = investorChats.find((c) => c.id === activeId) ?? investorChats[0];

  return (
    <div className="animate-fade-in">
      <WebPageHeader title="المحادثات" desc="محادثاتك مع المنشآت حول عروضك الاستثمارية" crumb="المحادثات" />

      <Container className="py-8">
        <div className="flex flex-col lg:flex-row gap-6 items-stretch">
          {/* Conversation list */}
          <aside className="w-full lg:w-[360px] shrink-0">
            <WCard className="p-2 lg:max-h-[620px] lg:overflow-y-auto">
              {investorChats.map((c) => (
                <button
                  key={c.id}
                  type="button"
                  onClick={() => setActiveId(c.id)}
                  className={cn(
                    "w-full flex items-center gap-3 rounded-[16px] p-3 text-start cursor-pointer transition-colors",
                    activeId === c.id ? "bg-background-200" : "hover:bg-background-50",
                  )}
                >
                  <div className="relative w-12 h-12 rounded-full overflow-hidden bg-background-200 shrink-0">
                    <img src={c.logo} alt={c.title} className="w-full h-full object-cover object-top" />
                    {c.unread > 0 && (
                      <span className="absolute -top-0.5 -start-0.5 min-w-[18px] h-[18px] px-1 rounded-full bg-accent-700 text-background-50 text-[10px] font-bold flex items-center justify-center">
                        {c.unread}
                      </span>
                    )}
                  </div>
                  <div className="flex-1 min-w-0">
                    <div className="flex items-center justify-between gap-2">
                      <p className="text-[14px] font-semibold text-foreground-950 truncate">{c.title}</p>
                      <span className="text-[11px] text-foreground-500 shrink-0">{c.time}</span>
                    </div>
                    <div className="mt-1">
                      <StatusPill tone={c.dealStatusTone}>{c.dealStatusLabel}</StatusPill>
                    </div>
                    <p className="text-[12px] text-foreground-600 truncate mt-1.5">{c.lastMessage}</p>
                  </div>
                </button>
              ))}
            </WCard>
          </aside>

          {/* Conversation */}
          <section className="flex-1 min-w-0">
            <WCard className="h-[620px] flex flex-col overflow-hidden">
              <div className="flex items-center gap-3 px-5 py-4 border-b border-background-200">
                <div className="w-11 h-11 rounded-full overflow-hidden bg-background-200 shrink-0">
                  <img src={active.logo} alt={active.title} className="w-full h-full object-cover object-top" />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="text-[15px] font-semibold text-foreground-950 truncate">{active.title}</p>
                  <StatusPill tone={active.dealStatusTone} className="mt-1">{active.dealStatusLabel}</StatusPill>
                </div>
                <WBtn
                  size="sm"
                  variant="secondary"
                  icon="ri-store-2-line"
                  onClick={() => navigate("business", { businessId: active.businessId })}
                >
                  صفحة المنشأة
                </WBtn>
              </div>

              {/* Messages */}
              <div className="flex-1 overflow-y-auto px-5 py-5 space-y-4 bg-background-50">
                {investorMessages.map((m) => {
                  if (m.type === "system") {
                    return (
                      <div key={m.id} className="flex justify-center">
                        <span className="rounded-full bg-secondary-100 text-secondary-900 px-4 py-1.5 text-[11.5px] font-semibold">
                          {m.text}
                        </span>
                      </div>
                    );
                  }
                  const mine = m.from === "me";
                  return (
                    <div key={m.id} className={cn("flex", mine ? "justify-end" : "justify-start")}>
                      <div
                        className={cn(
                          "max-w-[70%] rounded-[16px] px-4 py-2.5 text-[13.5px] leading-relaxed",
                          mine
                            ? "bg-primary-500 text-background-50"
                            : "bg-background-100 border border-background-200 text-foreground-900",
                        )}
                      >
                        {m.type === "file" ? (
                          <span className="flex items-center gap-2">
                            <i className="ri-file-pdf-2-line text-[18px] leading-none" />
                            {m.text}
                          </span>
                        ) : (
                          m.text
                        )}
                        <span className={cn("block text-[10.5px] mt-1", mine ? "text-background-50/65" : "text-foreground-500")}>
                          {m.time}
                        </span>
                      </div>
                    </div>
                  );
                })}
              </div>

              {/* Composer */}
              <div className="px-5 py-4 border-t border-background-200 flex items-center gap-3">
                <button
                  type="button"
                  aria-label="إرفاق ملف"
                  className="w-10 h-10 rounded-full hover:bg-background-200 flex items-center justify-center cursor-pointer shrink-0"
                >
                  <i className="ri-attachment-2 text-[20px] text-foreground-600 leading-none" />
                </button>
                <input
                  value={input}
                  onChange={(e) => setInput(e.target.value)}
                  placeholder="اكتب رسالتك..."
                  className="flex-1 h-11 px-4 rounded-[12px] bg-background-50 border border-background-200 text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 transition-colors font-body"
                />
                <WBtn icon="ri-send-plane-fill" disabled={!input.trim()} onClick={() => setInput("")}>
                  إرسال
                </WBtn>
              </div>
            </WCard>
          </section>
        </div>
      </Container>
    </div>
  );
}