"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServicesCalendarRepository = void 0;
const const_1 = require("../../../../schema-definitions/const");
const ropid_gtfs_1 = require("../../../../schema-definitions/ropid-gtfs");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const models_1 = require("@golemio/core/dist/integration-engine/models");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const SourceTableSuffixEnum_1 = require("../../helpers/SourceTableSuffixEnum");
class ServicesCalendarRepository extends models_1.PostgresModel {
    constructor() {
        super("ServicesCalendarRepository", {
            outputSequelizeAttributes: ropid_gtfs_1.RopidGTFS.servicesCalendar.outputSequelizeAttributes,
            pgTableName: ropid_gtfs_1.RopidGTFS.servicesCalendar.pgTableName,
            pgSchema: const_1.PG_SCHEMA,
            savingType: "insertOnly",
            attributesToRemove: ["id", "created_at", "updated_at"],
        }, new golemio_validator_1.JSONSchemaValidator("ServicesCalendarRepositoryValidator", ropid_gtfs_1.RopidGTFS.servicesCalendar.outputJsonSchema));
        this.populate = async (sourceTableSuffix) => {
            const sql = `
            SELECT
                dates.date,
                date_part('day', dates.date - to_char(timezone('Europe/Prague', now()), 'YYYY-MM-DD')::timestamp) AS day_diff,
                service_id
            FROM (
                SELECT
                    date_trunc('day', dd.dd)::date AS date
                FROM generate_series(
                    ( SELECT (CURRENT_TIMESTAMP - interval '1 day') )::date,
                    ( SELECT (CURRENT_TIMESTAMP + interval '2 days') )::date,
                    '1 day'
                ) dd(dd)) dates
            INNER JOIN "${const_1.PG_SCHEMA}".ropidgtfs_calendar${sourceTableSuffix} calendar ON ((1 = 1))
            WHERE (
                (
                    dates.date >= "start_date"::date
                    AND dates.date <= end_date::date
                    AND (
                        (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'monday'
                            AND monday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'tuesday'
                            AND tuesday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'wednesday'
                            AND wednesday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'thursday'
                            AND thursday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'friday'
                            AND friday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'saturday'
                            AND saturday = 1
                        ) OR (
                            btrim(to_char(dates.date::timestamptz, 'day')) = 'sunday'
                            AND sunday = 1
                        )
                    ) AND (
                        NOT service_id IN (
                            SELECT service_id
                            FROM "${const_1.PG_SCHEMA}".ropidgtfs_calendar_dates${sourceTableSuffix} calendar_dates
                            WHERE (
                                exception_type = 2
                                AND dates.date = calendar_dates.date::date
                            )
                        )
                    )) OR service_id IN (
                        SELECT service_id
                        FROM "${const_1.PG_SCHEMA}".ropidgtfs_calendar_dates${sourceTableSuffix} calendar_dates
                        WHERE
                            calendar_dates.exception_type = 1
                            AND dates.date = calendar_dates.date::date
                    )
                )
            ORDER BY dates.date
        `;
            try {
                const tmpTable = ropid_gtfs_1.RopidGTFS.servicesCalendar.pgTableName + SourceTableSuffixEnum_1.SourceTableSuffixEnum.Tmp;
                await this.sequelizeModel.sequelize.query(`INSERT INTO "${const_1.PG_SCHEMA}".${tmpTable} ${sql};`);
            }
            catch (err) {
                helpers_1.log.error(err);
                throw err;
            }
        };
    }
}
exports.ServicesCalendarRepository = ServicesCalendarRepository;
//# sourceMappingURL=ServicesCalendarRepository.js.map