"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.BackingService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const backing_logo_entity_1 = require("./backing-logo.entity");
const fs = require("fs");
const path = require("path");
function isUUID(v) { return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(v); }
let BackingService = class BackingService {
    constructor(repo) {
        this.repo = repo;
    }
    findAll() { return this.repo.find({ order: { order_index: "ASC" } }); }
    create(name, file) {
        return this.repo.save(this.repo.create({ name, image_url: `/uploads/${file.filename}` }));
    }
    async update(id, dto, file) {
        if (!isUUID(id))
            throw new common_1.NotFoundException("Logo no encontrado");
        const item = await this.repo.findOne({ where: { id } });
        if (!item)
            throw new common_1.NotFoundException("Logo no encontrado");
        if (dto.name !== undefined)
            item.name = dto.name;
        if (dto.is_active !== undefined)
            item.is_active = dto.is_active;
        if (file) {
            const old = path.join(__dirname, "..", "..", "uploads", item.image_url.replace("/uploads/", ""));
            try {
                if (fs.existsSync(old))
                    fs.unlinkSync(old);
            }
            catch { }
            item.image_url = `/uploads/${file.filename}`;
        }
        return this.repo.save(item);
    }
    async remove(id) {
        if (!isUUID(id))
            throw new common_1.NotFoundException("Logo no encontrado");
        const item = await this.repo.findOne({ where: { id } });
        if (!item)
            throw new common_1.NotFoundException("Logo no encontrado");
        const fp = path.join(__dirname, "..", "..", "uploads", item.image_url.replace("/uploads/", ""));
        try {
            if (fs.existsSync(fp))
                fs.unlinkSync(fp);
        }
        catch { }
        await this.repo.remove(item);
        return { message: "Logo eliminado" };
    }
};
exports.BackingService = BackingService;
exports.BackingService = BackingService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(backing_logo_entity_1.BackingLogo)),
    __metadata("design:paramtypes", [typeorm_2.Repository])
], BackingService);
//# sourceMappingURL=backing.service.js.map