import { NextRequest, NextResponse } from 'next/server';
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';
import { getDatabaseErrorMessage } from '@/lib/db-errors';
import {
  getEmployeeByIdForOrganization,
  softDeleteEmployeeForOrganization,
  updateEmployeeForOrganization
} from '@/features/employees/server/employee-data';
import type { EmployeeMutationPayload } from '@/features/employees/api/types';

export async function GET(
  _request: NextRequest,
  context: { params: Promise<{ id: string }> }
) {
  try {
    const { organization } = await requireOrganizationAccess();
    const params = await context.params;
    const employee = await getEmployeeByIdForOrganization({
      id: Number(params.id),
      organizationId: organization.id
    });

    if (!employee) {
      return NextResponse.json({ message: 'Employee not found' }, { status: 404 });
    }

    return NextResponse.json({
      success: true,
      time: new Date().toISOString(),
      message: 'Employee loaded successfully',
      employee
    });
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: getDatabaseErrorMessage(error) }, { status: 500 });
  }
}

export async function PATCH(
  request: NextRequest,
  context: { params: Promise<{ id: string }> }
) {
  try {
    const { organization } = await requireOrganizationAccess();
    const params = await context.params;
    const body = (await request.json()) as EmployeeMutationPayload;
    const employee = await updateEmployeeForOrganization({
      id: Number(params.id),
      organizationId: organization.id,
      payload: body
    });

    if (!employee) {
      return NextResponse.json({ message: 'Employee not found' }, { status: 404 });
    }

    return NextResponse.json({
      success: true,
      time: new Date().toISOString(),
      message: 'Employee updated successfully',
      employee
    });
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: getDatabaseErrorMessage(error) }, { status: 500 });
  }
}

export async function DELETE(
  _request: NextRequest,
  context: { params: Promise<{ id: string }> }
) {
  try {
    const { organization } = await requireOrganizationAccess();
    const params = await context.params;

    await softDeleteEmployeeForOrganization({
      id: Number(params.id),
      organizationId: organization.id
    });

    return NextResponse.json({
      success: true,
      time: new Date().toISOString(),
      message: 'Employee deactivated successfully'
    });
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: getDatabaseErrorMessage(error) }, { status: 500 });
  }
}
