"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RopidGTFSTripsModel = void 0;
const const_1 = require("../../schema-definitions/const");
const ropid_gtfs_1 = require("../../schema-definitions/ropid-gtfs");
const TripDto_1 = require("../../schema-definitions/ropid-gtfs/models/TripDto");
const models_1 = require("@golemio/core/dist/integration-engine/models");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const sequelize_1 = __importDefault(require("@golemio/core/dist/shared/sequelize"));
const RopidGTFSShapesModel_1 = require("./RopidGTFSShapesModel");
const RopidGTFSStopsModel_1 = require("./RopidGTFSStopsModel");
const RopidGTFSStopTimesModel_1 = require("./RopidGTFSStopTimesModel");
class RopidGTFSTripsModel extends models_1.PostgresModel {
    constructor() {
        super(ropid_gtfs_1.RopidGTFS.trips.name + "Model", {
            outputSequelizeAttributes: ropid_gtfs_1.RopidGTFS.trips.outputSequelizeAttributes,
            pgTableName: ropid_gtfs_1.RopidGTFS.trips.pgTableName,
            pgSchema: const_1.PG_SCHEMA,
            savingType: "insertOnly",
        }, new golemio_validator_1.JSONSchemaValidator(ropid_gtfs_1.RopidGTFS.trips.name + "ModelValidator", TripDto_1.TripDto.jsonSchema));
        this.findByIdForDelayComputation = async (tripId) => {
            const gtfsTripEntity = await this.sequelizeModel.findOne({
                attributes: ["shape_id"],
                include: [
                    {
                        attributes: [
                            "arrival_time",
                            "departure_time",
                            "shape_dist_traveled",
                            "stop_headsign",
                            "stop_id",
                            "stop_sequence",
                            [sequelize_1.default.literal(`EXTRACT(EPOCH FROM "arrival_time"::INTERVAL)::int`), "arrival_time_seconds"],
                            [sequelize_1.default.literal(`EXTRACT(EPOCH FROM "departure_time"::INTERVAL)::int`), "departure_time_seconds"],
                            [
                                sequelize_1.default.literal(`CASE when drop_off_type = '1' and pickup_type = '1' then true else false END`),
                                "is_no_stop_waypoint",
                            ],
                        ],
                        as: "stop_times",
                        model: this.modelGTFSStopTimes.sequelizeModel,
                        include: [
                            {
                                attributes: ["stop_id", "stop_lat", "stop_lon", "stop_name", "zone_id", "wheelchair_boarding"],
                                as: "stop",
                                model: this.modelGTFSStops.sequelizeModel,
                                required: true,
                            },
                        ],
                    },
                    {
                        as: "shapes",
                        model: this.modelGTFSShapes.sequelizeModel,
                        attributes: ["shape_dist_traveled", "shape_id", "shape_pt_lat", "shape_pt_lon", "shape_pt_sequence"],
                    },
                ],
                order: [
                    ["stop_times", "stop_sequence", "ASC"],
                    ["shapes", "shape_pt_sequence", "ASC"],
                ],
                where: { trip_id: tripId },
            });
            if (!gtfsTripEntity) {
                return null;
            }
            return gtfsTripEntity.toJSON();
        };
        this.modelGTFSStopTimes = new RopidGTFSStopTimesModel_1.RopidGTFSStopTimesModel();
        this.modelGTFSStops = new RopidGTFSStopsModel_1.RopidGTFSStopsModel();
        this.modelGTFSShapes = new RopidGTFSShapesModel_1.RopidGTFSShapesModel();
        this.sequelizeModel.hasMany(this.modelGTFSStopTimes.sequelizeModel, {
            as: "stop_times",
            foreignKey: "trip_id",
            sourceKey: "trip_id",
            constraints: false,
        });
        this.modelGTFSStopTimes.sequelizeModel.hasOne(this.modelGTFSStops.sequelizeModel, {
            as: "stop",
            foreignKey: "stop_id",
            sourceKey: "stop_id",
            constraints: false,
        });
        this.sequelizeModel.hasMany(this.modelGTFSShapes.sequelizeModel, {
            as: "shapes",
            foreignKey: "shape_id",
            sourceKey: "shape_id",
            constraints: false,
        });
    }
}
exports.RopidGTFSTripsModel = RopidGTFSTripsModel;
//# sourceMappingURL=RopidGTFSTripsModel.js.map