const express = require('express');

const wrapAsync = require('../helpers').wrapAsync;
const { resolveTenantContextByHost } = require('../tenantContext');

const router = express.Router();

/**
 *  @swagger
 *  /api/organizations:
 *    get:
 *      security:
 *        - bearerAuth: []
 *      tags: [Organizations]
 *      summary: Get all organizations
 *      description: Get all organizations
 *      responses:
 *        200:
 *          description: Organizations list successfully received
 *          content:
 *            application/json:
 *              schema:
 *                type: array
 *                items:
 *                  $ref: "#/components/schemas/Organizations"
 *        401:
 *          $ref: "#/components/responses/UnauthorizedError"
 *        404:
 *          description: Data not found
 *        500:
 *          description: Some server error
 */

router.get(
  '/',
  wrapAsync(async (req, res) => {
    const { organization, tenant, subdomain } = await resolveTenantContextByHost(req, {
      required: true,
    });

    res.status(200).send([
      {
        id: organization.id,
        name: organization.name,
        subdomain,
        tenantId: tenant.id,
      },
    ]);
  }),
);

router.use('/', require('../helpers').commonErrorHandler);

module.exports = router;
