"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecondaryBannersService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const secondary_banner_entity_1 = require("./secondary-banner.entity");
const fs = require("fs");
const path = require("path");
function isUUID(val) { return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(val); }
let SecondaryBannersService = class SecondaryBannersService {
    constructor(repo) {
        this.repo = repo;
    }
    findAll() { return this.repo.find({ order: { type: "ASC", order_index: "ASC" } }); }
    async findByType(type) { return this.repo.find({ where: { type, is_active: true }, order: { order_index: "ASC" } }); }
    async create(dto, file) {
        return this.repo.save(this.repo.create({
            title: dto.title, subtitle: dto.subtitle, type: dto.type,
            image_url: `/uploads/${file.filename}`,
            link_url: dto.link_url, button_text: dto.button_text,
        }));
    }
    async update(id, dto, file) {
        if (!isUUID(id))
            throw new common_1.NotFoundException("Banner no encontrado");
        const b = await this.repo.findOne({ where: { id } });
        if (!b)
            throw new common_1.NotFoundException("Banner no encontrado");
        if (dto.title !== undefined)
            b.title = dto.title;
        if (dto.subtitle !== undefined)
            b.subtitle = dto.subtitle;
        if (dto.link_url !== undefined)
            b.link_url = dto.link_url;
        if (dto.button_text !== undefined)
            b.button_text = dto.button_text;
        if (dto.is_active !== undefined)
            b.is_active = dto.is_active;
        if (dto.type !== undefined)
            b.type = dto.type;
        if (file) {
            const oldPath = path.join(__dirname, "..", "..", "uploads", b.image_url.replace("/uploads/", ""));
            try {
                if (fs.existsSync(oldPath))
                    fs.unlinkSync(oldPath);
            }
            catch { }
            b.image_url = `/uploads/${file.filename}`;
        }
        return this.repo.save(b);
    }
    async remove(id) {
        if (!isUUID(id))
            throw new common_1.NotFoundException("Banner no encontrado");
        const b = await this.repo.findOne({ where: { id } });
        if (!b)
            throw new common_1.NotFoundException("Banner no encontrado");
        const fp = path.join(__dirname, "..", "..", "uploads", b.image_url.replace("/uploads/", ""));
        try {
            if (fs.existsSync(fp))
                fs.unlinkSync(fp);
        }
        catch { }
        await this.repo.remove(b);
        return { message: "Banner eliminado" };
    }
};
exports.SecondaryBannersService = SecondaryBannersService;
exports.SecondaryBannersService = SecondaryBannersService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(secondary_banner_entity_1.SecondaryBanner)),
    __metadata("design:paramtypes", [typeorm_2.Repository])
], SecondaryBannersService);
//# sourceMappingURL=secondary-banners.service.js.map