"use client";

import { RiAddFill, RiTodoLine } from "@remixicon/react";
import { useState } from "react";
import { toast } from "sonner";
import { archiveNoteAction, deleteNoteAction } from "@/features/notes/actions";
import { ConfirmActionDialog } from "@/shared/components/confirm-action-dialog";
import { EmptyState } from "@/shared/components/feedback/empty-state";
import { Button } from "@/shared/components/ui/button";
import { Card } from "@/shared/components/ui/card";
import {
	Tabs,
	TabsContent,
	TabsList,
	TabsTrigger,
} from "@/shared/components/ui/tabs";
import { NoteCard } from "./note-card";
import { NoteDetailsDialog } from "./note-details-dialog";
import { NoteDialog } from "./note-dialog";
import type { Note } from "./types";

interface NotesPageProps {
	notes: Note[];
	archivedNotes: Note[];
	attachmentMaxSizeMb?: number;
}

export function NotesPage({
	notes,
	archivedNotes,
	attachmentMaxSizeMb,
}: NotesPageProps) {
	const [activeTab, setActiveTab] = useState("ativas");
	const [createOpen, setCreateOpen] = useState(false);
	const [editOpen, setEditOpen] = useState(false);
	const [noteToEdit, setNoteToEdit] = useState<Note | null>(null);
	const [detailsOpen, setDetailsOpen] = useState(false);
	const [noteDetails, setNoteDetails] = useState<Note | null>(null);
	const [removeOpen, setRemoveOpen] = useState(false);
	const [noteToRemove, setNoteToRemove] = useState<Note | null>(null);
	const [arquivarOpen, setArquivarOpen] = useState(false);
	const [noteToArquivar, setNoteToArquivar] = useState<Note | null>(null);

	const sortedNotes = notes;
	const sortedArchivedNotes = archivedNotes;

	const isArquivadas = activeTab === "arquivadas";

	const handleCreateOpenChange = (open: boolean) => {
		setCreateOpen(open);
	};

	const handleEditOpenChange = (open: boolean) => {
		setEditOpen(open);
		if (!open) {
			setNoteToEdit(null);
		}
	};

	const handleDetailsOpenChange = (open: boolean) => {
		setDetailsOpen(open);
		if (!open) {
			setNoteDetails(null);
		}
	};

	const handleRemoveOpenChange = (open: boolean) => {
		setRemoveOpen(open);
		if (!open) {
			setNoteToRemove(null);
		}
	};

	const handleArquivarOpenChange = (open: boolean) => {
		setArquivarOpen(open);
		if (!open) {
			setNoteToArquivar(null);
		}
	};

	const handleEditRequest = (note: Note) => {
		setNoteToEdit(note);
		setEditOpen(true);
	};

	const handleDetailsRequest = (note: Note) => {
		setNoteDetails(note);
		setDetailsOpen(true);
	};

	const handleRemoveRequest = (note: Note) => {
		setNoteToRemove(note);
		setRemoveOpen(true);
	};

	const handleArquivarRequest = (note: Note) => {
		setNoteToArquivar(note);
		setArquivarOpen(true);
	};

	const handleArquivarConfirm = async () => {
		if (!noteToArquivar) {
			return;
		}

		const result = await archiveNoteAction({
			id: noteToArquivar.id,
			archived: !isArquivadas,
		});

		if (result.success) {
			toast.success(result.message);
			return;
		}

		toast.error(result.error);
		throw new Error(result.error);
	};

	const handleRemoveConfirm = async () => {
		if (!noteToRemove) {
			return;
		}

		const result = await deleteNoteAction({ id: noteToRemove.id });

		if (result.success) {
			toast.success(result.message);
			return;
		}

		toast.error(result.error);
		throw new Error(result.error);
	};

	const removeTitle = noteToRemove
		? noteToRemove.title.trim().length
			? `Remover anotação "${noteToRemove.title}"?`
			: "Remover anotação?"
		: "Remover anotação?";

	const arquivarTitle = noteToArquivar
		? noteToArquivar.title.trim().length
			? isArquivadas
				? `Desarquivar anotação "${noteToArquivar.title}"?`
				: `Arquivar anotação "${noteToArquivar.title}"?`
			: isArquivadas
				? "Desarquivar anotação?"
				: "Arquivar anotação?"
		: isArquivadas
			? "Desarquivar anotação?"
			: "Arquivar anotação?";

	const renderNoteList = (list: Note[], isArchived: boolean) => {
		if (list.length === 0) {
			return (
				<Card className="flex min-h-[50vh] w-full items-center justify-center py-12">
					<EmptyState
						media={<RiTodoLine className="size-6 text-primary" />}
						title={
							isArchived
								? "Nenhuma anotação archived"
								: "Nenhuma anotação registrada"
						}
						description={
							isArchived
								? "As anotações arquivadas aparecerão aqui."
								: "Crie anotações personalizadas para acompanhar lembretes, decisões ou observações financeiras importantes."
						}
					/>
				</Card>
			);
		}

		return (
			<div className="grid gap-4 grid-cols-1 sm:grid-cols-2 xl:grid-cols-3">
				{list.map((note) => (
					<NoteCard
						key={note.id}
						note={note}
						onEdit={handleEditRequest}
						onDetails={handleDetailsRequest}
						onRemove={handleRemoveRequest}
						onArquivar={handleArquivarRequest}
						isArquivadas={isArchived}
					/>
				))}
			</div>
		);
	};

	return (
		<>
			<div className="flex w-full flex-col gap-6">
				<div className="flex">
					<NoteDialog
						mode="create"
						open={createOpen}
						onOpenChange={handleCreateOpenChange}
						attachmentMaxSizeMb={attachmentMaxSizeMb}
						trigger={
							<Button className="w-full sm:w-auto">
								<RiAddFill className="size-4" />
								Nova anotação
							</Button>
						}
					/>
				</div>

				<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
					<TabsList>
						<TabsTrigger value="ativas">Ativas</TabsTrigger>
						<TabsTrigger value="arquivadas">Arquivadas</TabsTrigger>
					</TabsList>

					<TabsContent value="ativas" className="mt-4">
						{renderNoteList(sortedNotes, false)}
					</TabsContent>

					<TabsContent value="arquivadas" className="mt-4">
						{renderNoteList(sortedArchivedNotes, true)}
					</TabsContent>
				</Tabs>
			</div>

			<NoteDialog
				mode="update"
				note={noteToEdit ?? undefined}
				open={editOpen}
				onOpenChange={handleEditOpenChange}
				attachmentMaxSizeMb={attachmentMaxSizeMb}
			/>

			<NoteDetailsDialog
				note={noteDetails}
				open={detailsOpen}
				onOpenChange={handleDetailsOpenChange}
				onEdit={handleEditRequest}
			/>

			<ConfirmActionDialog
				open={arquivarOpen}
				onOpenChange={handleArquivarOpenChange}
				title={arquivarTitle}
				description={
					isArquivadas
						? "A anotação será movida de volta para a lista principal."
						: "A anotação será movida para arquivadas."
				}
				confirmLabel={isArquivadas ? "Desarquivar" : "Arquivar"}
				confirmVariant="default"
				pendingLabel={isArquivadas ? "Desarquivando..." : "Arquivando..."}
				onConfirm={handleArquivarConfirm}
			/>

			<ConfirmActionDialog
				open={removeOpen}
				onOpenChange={handleRemoveOpenChange}
				title={removeTitle}
				description="Essa ação não pode ser desfeita."
				confirmLabel="Remover"
				confirmVariant="destructive"
				pendingLabel="Removendo..."
				onConfirm={handleRemoveConfirm}
			/>
		</>
	);
}
