import { ViewLocationCard } from '@/app/ui/ViewLocationCard';
import { fetchLocationById, setSeenByTenant } from '@/app/lib/actions/locationActions';
import { notFound } from 'next/navigation';
import { myAuth } from '@/app/lib/auth';

export default async function LocationViewPage({ locationId }: { locationId:string }) {
    const location = await fetchLocationById(locationId);

    if (!location) {
        return(notFound());
    }

    // Check if the page was accessed by an authenticated user who is the owner
    const session = await myAuth();
    const isOwner = session?.user?.id === location.userId;
  
    // If the page is not visited by the owner, mark it as seen by tenant
    if (!isOwner) {
        await setSeenByTenant(locationId);
    }

    return (<ViewLocationCard location={location} />);
}