import { NextRequest, NextResponse } from 'next/server'
import { LocalStorageProvider } from '@/lib/storage/local-provider'
import { getContentType } from '@/lib/storage'
import * as fs from 'fs/promises'

const provider = new LocalStorageProvider()

/**
 * Handle GET requests for file downloads
 */
export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url)
  const key = searchParams.get('key')
  const action = searchParams.get('action')
  const expires = searchParams.get('expires')
  const sig = searchParams.get('sig')

  // Validate required parameters
  if (!key || !action || !expires || !sig) {
    return NextResponse.json(
      { error: 'Missing required parameters' },
      { status: 400 }
    )
  }

  // Verify signature and expiry
  const isValid = LocalStorageProvider.verifySignature(
    key,
    action,
    parseInt(expires),
    sig
  )

  if (!isValid) {
    return NextResponse.json(
      { error: 'Invalid or expired signature' },
      { status: 403 }
    )
  }

  if (action !== 'download') {
    return NextResponse.json(
      { error: 'Invalid action for GET request' },
      { status: 400 }
    )
  }

  try {
    const filePath = provider.getAbsoluteFilePath(key)
    const data = await fs.readFile(filePath)
    const contentType = getContentType(key)

    return new NextResponse(data, {
      headers: {
        'Content-Type': contentType,
        'Cache-Control': 'private, max-age=3600',
      },
    })
  } catch (error) {
    if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
      return NextResponse.json({ error: 'File not found' }, { status: 404 })
    }
    console.error('Error serving file:', error)
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    )
  }
}

/**
 * Handle PUT requests for file uploads
 */
export async function PUT(request: NextRequest) {
  const { searchParams } = new URL(request.url)
  const key = searchParams.get('key')
  const action = searchParams.get('action')
  const expires = searchParams.get('expires')
  const sig = searchParams.get('sig')

  // Validate required parameters
  if (!key || !action || !expires || !sig) {
    return NextResponse.json(
      { error: 'Missing required parameters' },
      { status: 400 }
    )
  }

  // Verify signature and expiry
  const isValid = LocalStorageProvider.verifySignature(
    key,
    action,
    parseInt(expires),
    sig
  )

  if (!isValid) {
    return NextResponse.json(
      { error: 'Invalid or expired signature' },
      { status: 403 }
    )
  }

  if (action !== 'upload') {
    return NextResponse.json(
      { error: 'Invalid action for PUT request' },
      { status: 400 }
    )
  }

  try {
    const contentType = request.headers.get('content-type') || 'application/octet-stream'
    const data = Buffer.from(await request.arrayBuffer())

    await provider.putObject(key, data, contentType)

    return NextResponse.json({ success: true }, { status: 200 })
  } catch (error) {
    console.error('Error uploading file:', error)
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    )
  }
}
