"use server"
import BoardDelete from "@/components/BoardDelete";
import EmailAccessList from "@/components/EmailAccessList";
import NewUserAccess from "@/components/forms/NewUserAccess";
import { liveblocksClient } from "@/lib/liveblocksClient";
import { getUserEmail } from "@/lib/userClient";
import Link from "next/link";
import { ImBackward2 } from "react-icons/im";


type PageProps ={
    params: {
        boardId: string;
    }
}

const page = async ({params}: PageProps) => {
    const {boardId} = params;
    const boardInfo = await liveblocksClient.getRoom(boardId);
    const userEmail = await getUserEmail()
    if(!boardInfo.usersAccesses[userEmail]){
        return 'Access denied';
    }
    

  return (
    <div>
        <div className="flex justify-between">
           <Link href={`/boards/${boardId}`} className="flex items-center gap-1 text-[#164863] font-semibold hover:text-[#427D9D] hover:duration-300"><ImBackward2/>Go back</Link> 
           <BoardDelete boardId={boardId}/>
        </div>
        <h1 className="text-2xl mb-2">Access to {boardInfo.metadata.boardName}:</h1>
        <div className="mb-6">
            <EmailAccessList boardId={boardId} usersAccesses={boardInfo.usersAccesses}/>
        </div>
        <NewUserAccess boardId = {boardId}/>
    </div>
  )
}

export default page