import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
import { TeamService } from '@/lib/services/teams/TeamService';
import { TeamsApiClient } from '@/lib/api/teams/TeamsApiClient';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { notFound } from 'next/navigation';
import { TeamDetailsViewModel } from '@/lib/view-models/TeamDetailsViewModel';
import { TeamMemberViewModel } from '@/lib/view-models/TeamMemberViewModel';
import TeamDetailTemplate from '@/templates/TeamDetailTemplate';

// Template wrapper to adapt TeamDetailTemplate for SSR
interface TeamDetailData {
  team: TeamDetailsViewModel;
  memberships: TeamMemberViewModel[];
  isAdmin: boolean;
}

function TeamDetailTemplateWrapper({ data }: { data: TeamDetailData }) {
  return (
    <TeamDetailTemplate
      team={data.team}
      memberships={data.memberships}
      activeTab="overview"
      loading={false}
      isAdmin={data.isAdmin}
      // Event handlers are no-ops for SSR (client will handle real interactions)
      onTabChange={() => {}}
      onUpdate={() => {}}
      onRemoveMember={() => {}}
      onChangeRole={() => {}}
      onGoBack={() => {}}
    />
  );
}

export default async function Page({ params }: { params: { id: string } }) {
  // Validate params
  if (!params.id) {
    notFound();
  }

  // Fetch data using PageDataFetcher.fetchManual
  const data = await PageDataFetcher.fetchManual(async () => {
    // Manual dependency creation
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new EnhancedErrorReporter(logger, {
      showUserNotifications: true,
      logToConsole: true,
      reportToExternal: process.env.NODE_ENV === 'production',
    });

    // Create API client
    const teamsApiClient = new TeamsApiClient(baseUrl, errorReporter, logger);

    // Create service
    const service = new TeamService(teamsApiClient);
    
    // For server-side, we need a current driver ID
    // This would typically come from session, but for server components we'll use a placeholder
    const currentDriverId = ''; // Placeholder - would need session handling
    
    // Fetch team details
    const teamData = await service.getTeamDetails(params.id, currentDriverId);
    
    if (!teamData) {
      return null;
    }

    // Fetch team members
    const membersData = await service.getTeamMembers(params.id, currentDriverId, teamData.ownerId || '');
    
    // Determine admin status
    const isAdmin = teamData.isOwner ||
      (membersData || []).some((m: any) => m.driverId === currentDriverId && (m.role === 'manager' || m.role === 'owner'));

    return {
      team: teamData,
      memberships: membersData || [],
      isAdmin,
    };
  });

  if (!data) {
    notFound();
  }

  return (
    <PageWrapper
      data={data}
      Template={TeamDetailTemplateWrapper}
      loading={{ variant: 'skeleton', message: 'Loading team details...' }}
      errorConfig={{ variant: 'full-screen' }}
      empty={{
        title: 'Team not found',
        description: 'The team you are looking for does not exist or has been removed.',
      }}
    />
  );
}