import fastify from "fastify"
import cors from "@fastify/cors"
import cookie from "@fastify/cookie"
import path, { dirname } from "path"
import { fileURLToPath } from "node:url"
import Multipart from "@fastify/multipart"
import FastifyStatic from "@fastify/static"
import rateLimit from "@fastify/rate-limit"
import { FileAction } from "./actions/file.action.js"
import { UserSchemas } from "./schemas/user.schema.js"
import { AuthSchemas } from "./schemas/auth.schema.js"
import { SkillSchemas } from "./schemas/skill.schema.js"
import { StackSchemas } from "./schemas/stack.schema.js"
import type { FastifyCookieOptions } from "@fastify/cookie"
import { FabricSchemas } from "./schemas/fabric.schema.js"
import { ProjectSchemas } from "./schemas/project.schema.js"
import { ChatGPTSchemas } from "./schemas/chat-gpt.schema.js"
import { FeedbackSchemas } from "./schemas/feedback.schema.js"
import { BuildTeamSchemas } from "./schemas/build-team.schema.js"
import { AuthController } from "./controllers/auth.controller.js"
import { UserController } from "./controllers/user.controller.js"
import { SkillController } from "./controllers/skill.controller.js"
import { StackController } from "./controllers/stack.controller.js"
import { FabricController } from "./controllers/fabric.controller.js"
import { ProjectController } from "./controllers/project.controller.js"
import { ChatGPTController } from "./controllers/chat-gpt.controller.js"
import { FeedbackController } from "./controllers/feedback.controller.js"
import { PaymentMethodSchemas } from "./schemas/payment-method.schema.js"
import { EngineerRegionSchemas } from "./schemas/engineer-region.schema.js"
import { BuildTeamController } from "./controllers/build-team.controller.js"
import { ProjectTopicAreaSchemas } from "./schemas/project-topic-area.schema.js"
import { PaymentMethodController } from "./controllers/payment-method.controller.js"
import { EngineerRegionController } from "./controllers/engineer-region.controller.js"
import { ProjectTopicAreaController } from "./controllers/project-topic-area.controller.js"

// setup fastify server
const app = fastify({ logger: true })

// ---------------------------------------------------------------------------
// registered fastify dependencies
// ---------------------------------------------------------------------------

app.register(rateLimit, {
  max: 10,
  timeWindow: 1000,
})

app.register(cookie, {
  parseOptions: {
    path: "/",
    secure: true,
    sameSite: "none",
  },
} as FastifyCookieOptions)

app.register(cors, {
  origin: [
    "https://devi-hub.com",
    "https://www.devi-hub.com",
    "http://devi-hub.com",
    "http://www.devi-hub.com",
    "http://localhost:3010",
  ],
  methods: ["GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"],
  optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
  credentials: true, //Credentials are authorization header
  allowedHeaders: [
    "token",
    "Origin",
    "Accept",
    "Content-Type",
    "Authorization",
    "X-Requested-With",
    "device-remember-token",
    "Access-Control-Allow-Origin",
  ],
})

app.register(Multipart)

app.register(FastifyStatic, {
  // root: path.join(dirname(__dirname), "./uploads"),
  root: path.join(dirname(fileURLToPath(import.meta.url)), "./uploads"),
  prefix: "/api/uploads/",
})

// ---------------------------------------------------------------------------
// fastify routs
// ---------------------------------------------------------------------------

app.post("/api/file/upload", FileAction.upload)

// --------------Auth--------------

app.post(
  "/api/auth/register",
  { schema: { body: AuthSchemas.register } },
  AuthController.register,
)
app.post(
  "/api/auth/login",
  { schema: { body: AuthSchemas.login } },
  AuthController.login,
)
app.post(
  "/api/auth/checkEmail",
  { schema: { body: AuthSchemas.checkEmail } },
  AuthController.checkEmail,
)

app.get("/api/auth/regenerateToken", AuthController.regenerateToken)

// --------------Stack--------------

app.post(
  "/api/stack/add",
  { schema: { body: StackSchemas.add } },
  StackController.add,
)
app.delete(
  "/api/stack/delete/:id",
  { schema: { params: StackSchemas.delete } },
  StackController.delete,
)
app.get("/api/stack/getAll", StackController.getAll)

// --------------Fabric--------------
app.post(
  "/api/fabric/add",
  { schema: { body: FabricSchemas.add } },
  FabricController.add,
)
app.delete(
  "/api/fabric/delete/:id",
  { schema: { params: FabricSchemas.delete } },
  FabricController.delete,
)
app.get("/api/fabric/getAll", FabricController.getAll)

// --------------BuildTeam--------------

app.post(
  "/api/buildTeam/addTeam",
  { schema: { body: BuildTeamSchemas.addTeam } },
  BuildTeamController.addTeam,
)
app.post(
  "/api/buildTeam/addProduct",
  { schema: { body: BuildTeamSchemas.addProduct } },
  BuildTeamController.addProduct,
)
app.get("/api/buildTeam/getTeams", BuildTeamController.getTeams)
app.get("/api/buildTeam/getProducts", BuildTeamController.getProducts)

// --------------Skill--------------

app.post(
  "/api/skill/add",
  { schema: { body: SkillSchemas.add } },
  SkillController.add,
)
app.delete(
  "/api/skill/delete/:id",
  { schema: { params: SkillSchemas.delete } },
  SkillController.delete,
)
app.get("/api/skill/getAll", SkillController.getAll)

// --------------PaymentMethod--------------

app.post(
  "/api/paymentMethod/add",
  { schema: { body: PaymentMethodSchemas.add } },
  PaymentMethodController.add,
)
app.delete(
  "/api/paymentMethod/delete/:id",
  { schema: { params: PaymentMethodSchemas.delete } },
  PaymentMethodController.delete,
)
app.get("/api/paymentMethod/getAll", PaymentMethodController.getAll)

// --------------ProjectTopicArea--------------

app.post(
  "/api/projectTopicArea/add",
  { schema: { body: ProjectTopicAreaSchemas.add } },
  ProjectTopicAreaController.add,
)
app.delete(
  "/api/projectTopicArea/delete/:id",
  { schema: { params: ProjectTopicAreaSchemas.delete } },
  ProjectTopicAreaController.delete,
)
app.get("/api/projectTopicArea/getAll", ProjectTopicAreaController.getAll)

// --------------EngineerRegion--------------

app.post(
  "/api/engineerRegion/add",
  { schema: { body: EngineerRegionSchemas.add } },
  EngineerRegionController.add,
)
app.delete(
  "/api/engineerRegion/delete/:id",
  { schema: { params: EngineerRegionSchemas.delete } },
  EngineerRegionController.delete,
)
app.get("/api/engineerRegion/getAll", EngineerRegionController.getAll)

// --------------Project--------------

app.put("/api/project/updateVerified", ProjectController.updateVerified)

app.post(
  "/api/project/add",
  { schema: { body: ProjectSchemas.add } },
  ProjectController.add,
)

app.get(
  "/api/project/getProject/:id",
  { schema: { params: ProjectSchemas.getProject } },
  ProjectController.getProjectById,
)
app.get("/api/project/getPublished", ProjectController.getPublished)
app.get("/api/project/getVerified", ProjectController.getVerified)
app.get("/api/project/getRejected", ProjectController.getRejected)

// --------------Feedback--------------

app.post(
  "/api/feedback/add",
  { schema: { body: FeedbackSchemas.add } },
  FeedbackController.add,
)
app.get("/api/feedback/get", FeedbackController.get)
app.put("/api/feedback/update", FeedbackController.update)

// --------------User--------------

app.post("/api/user/add", UserController.add)
app.post("/api/user/updatePassword", UserController.updatePassword)
app.put(
  "/api/user/editCompanyProfile",
  { schema: { body: UserSchemas.editCompanyProfile } },
  UserController.editCompanyProfile,
)
app.put(
  "/api/user/editEngineerProfile",
  { schema: { body: UserSchemas.editEngineerProfile } },
  UserController.editEngineerProfile,
)
app.get(
  "/api/user/getCompanyProfile/:id",
  { schema: { params: UserSchemas.getCompanyProfile } },
  UserController.getCompanyProfile,
)
app.get(
  "/api/user/getEngineerProfile/:id",
  { schema: { params: UserSchemas.getEngineerProfile } },
  UserController.getEngineerProfile,
)

// --------------Gpt--------------

app.post(
  "/api/gpt/buildTeam",
  { schema: { body: ChatGPTSchemas.buildTeam } },
  ChatGPTController.buildTeam,
)
app.post(
  "/api/gpt/buildProduct",
  { schema: { body: ChatGPTSchemas.buildProject } },
  ChatGPTController.buildProject,
)

// --------------tests--------------

// app.post("/api/talks", ChatGPTController.talks)

export default app
http://localhost/api/stack/add
Error: connect ECONNREFUSED 127.0.0.1:80 in postman