import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';

export async function PATCH(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session || session.user.role !== 'DRIVER') {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }

    const body = await request.json();
    const { status } = body;

    if (!status) {
      return NextResponse.json({ error: 'Status is required' }, { status: 400 });
    }

    const tourStop = await prisma.tourStop.findUnique({
      where: { id: params.id },
      include: {
        tour: {
          select: {
            driverId: true,
          },
        },
      },
    });

    if (!tourStop) {
      return NextResponse.json({ error: 'Tour stop not found' }, { status: 404 });
    }

    if (tourStop.tour.driverId !== session.user.id) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 403 });
    }

    const updateData: any = { status };

    switch (status) {
      case 'ARRIVED':
        updateData.arrivedAt = new Date();
        break;
      case 'SETUP_IN_PROGRESS':
        updateData.setupStartedAt = new Date();
        break;
      case 'SETUP_COMPLETE':
        updateData.setupCompleteAt = new Date();
        break;
      case 'PICKUP_IN_PROGRESS':
        updateData.pickupStartedAt = new Date();
        break;
      case 'PICKUP_COMPLETE':
        updateData.pickupCompleteAt = new Date();
        break;
    }

    const updatedStop = await prisma.tourStop.update({
      where: { id: params.id },
      data: updateData,
    });

    return NextResponse.json({ tourStop: updatedStop });
  } catch (error: any) {
    console.error('Status update error:', error);
    return NextResponse.json(
      { error: error.message || 'Failed to update status' },
      { status: 500 }
    );
  }
}
