import { Router } from "express"
import { addAsync } from "@awaitjs/express"
import { suggestPrChanges } from "../endpoints/suggest-pr-changes.js"
import { createPr, addRepositoryManually } from "../endpoints/create-pr.js"
import { verifyExistingOptimizations } from "../endpoints/verify-existing-optimizations.js"
import {
  add_optimized_code_context,
  is_code_being_optimized_again,
} from "../endpoints/code-context-hash.js"
import { optimizationSuccess } from "../endpoints/optimiaztion-success.js"
import { trackUsage } from "../middlewares/track-usage.js"
import createStagingReview from "../endpoints/create-staging.js"
import getStagingCode from "../endpoints/get-staging-code.js"
import commitStagingCode from "../endpoints/commit-staging-code.js"
import { ROUTES, USAGE_TRACKED_ROUTES } from "../constants/index.js"

const router = addAsync(Router()) as any

// Apply usage tracking to optimization endpoints
USAGE_TRACKED_ROUTES.forEach(endpoint => {
  router.use(endpoint, trackUsage)
})

// Optimization endpoints
router.postAsync(ROUTES.SUGGEST_PR_CHANGES, suggestPrChanges)
router.postAsync(ROUTES.CREATE_PR, createPr)
router.postAsync(ROUTES.VERIFY_EXISTING_OPTIMIZATIONS, verifyExistingOptimizations)
router.postAsync(ROUTES.IS_ALREADY_OPTIMIZED, is_code_being_optimized_again)
router.postAsync(ROUTES.ADD_CODE_HASH, add_optimized_code_context)
router.postAsync(ROUTES.MARK_AS_SUCCESS, optimizationSuccess)
router.postAsync(ROUTES.CREATE_STAGING, createStagingReview)
router.postAsync(ROUTES.GET_STAGING_CODE, getStagingCode)
router.postAsync(ROUTES.COMMIT_STAGING_CODE, commitStagingCode)
router.postAsync(ROUTES.TEST_REPO, addRepositoryManually)

export default router
