"use client";

import Image from "next/image";
import Link from "next/link";
import { UploadForm } from "../components/uploadForm";
import { FilesTable } from "../components/filesTable";
import { useState } from "react";

interface FileData {
  id: string;
  filename: string;
  mimetype: string;
  size: number;
  createdAt: string;
  updatedAt: string;
}

export default function FilesPage() {
  const [newFile, setNewFile] = useState<FileData | null>(null);

  const handleFileUploaded = (file: FileData) => {
    // This will trigger the FilesTable to add the new file
    setNewFile(file);
  };

  return (
    <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
      <main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
        <Image
          className="dark:invert"
          src="/vtorio.svg"
          alt="Vtor.io logo"
          width={180}
          height={38}
          priority
        />
        
        <h1 className="text-3xl font-bold text-center sm:text-left">
          File Management
        </h1>
        
        <p className="text-sm text-center sm:text-left max-w-[600px]">
          Upload and manage your media files here. Select files to upload them to the server.
        </p>

        <div className="flex flex-col gap-4 items-center sm:items-start">
          <h2 className="text-xl font-semibold">Upload Files</h2>
          <UploadForm onFileUploaded={handleFileUploaded} />
        </div>

        <div className="w-full max-w-6xl">
          <h2 className="text-2xl font-semibold mb-4">Files in Database</h2>
          <FilesTable onFileAdded={handleFileUploaded} />
        </div>
        
        <div className="flex gap-4 items-center flex-col sm:flex-row">
          <Link
            className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
            href="/"
          >
            Back to Home
          </Link>
          <Link
            className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
            href="/api-docs"
          >
            API Documentation
          </Link>
        </div>
      </main>
    </div>
  );
}