import type { Request as ExRequest } from "express";
import { Controller, Get, Path, Query, Request, Response, Route, Tags } from "tsoa";
import { getOrganizationSummary, listOrganizations, listProjects, listTasks } from "../repository";
import type {
  ErrorResponse,
  OrganizationEnvelope,
  OrganizationsEnvelope,
  ProjectsEnvelope,
  TasksEnvelope,
} from "../models";
import { HttpError } from "../httpErrors";
import { getDatabase } from "./shared";

@Route("orgs")
@Tags("Organizations")
export class OrganizationsController extends Controller {
  @Get()
  public listOrganizations(@Request() request: ExRequest): OrganizationsEnvelope {
    return { organizations: listOrganizations(getDatabase(request)) };
  }

  @Get("{orgId}")
  @Response<ErrorResponse>(404, "Organization not found")
  public getOrganization(
    @Request() request: ExRequest,
    @Path() orgId: string,
  ): OrganizationEnvelope {
    const organization = getOrganizationSummary(getDatabase(request), orgId);
    if (!organization) {
      throw new HttpError(404, "Organization not found.");
    }

    return { organization };
  }

  @Get("{orgId}/projects")
  public listOrganizationProjects(
    @Request() request: ExRequest,
    @Path() orgId: string,
  ): ProjectsEnvelope {
    return { projects: listProjects(getDatabase(request), orgId) };
  }

  @Get("{orgId}/tasks")
  public listOrganizationTasks(
    @Request() request: ExRequest,
    @Path() orgId: string,
    @Query() projectId?: string,
    @Query() status?: string,
    @Query() priority?: string,
    @Query() assigneeUserId?: string,
  ): TasksEnvelope {
    return {
      tasks: listTasks(getDatabase(request), {
        orgId,
        projectId,
        status,
        priority,
        assigneeUserId,
      }),
    };
  }
}
