"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.AdminShopService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("typeorm");
const typeorm_2 = require("@nestjs/typeorm");
const shop_entity_1 = require("../../common/shop/shop.entity");
const service_entity_1 = require("../../common/service/service.entity");
const user_entity_1 = require("../../appapi/user/user.entity");
const shopoffer_entity_1 = require("../../common/shopoffer/shopoffer.entity");
let AdminShopService = class AdminShopService {
    constructor(shopRepository, serviceRepository, userRepository) {
        this.shopRepository = shopRepository;
        this.serviceRepository = serviceRepository;
        this.userRepository = userRepository;
    }
    async create(createShopDto) {
        const { services, contact, ...shopData } = createShopDto;
        console.log(shopData);
        const shop = this.shopRepository.create({
            ...shopData,
            services: services.map((service) => Object.assign(new service_entity_1.Service(), service)),
            contact: contact ? contact : {
                "website": null,
                "email": null,
                "phoneNumber": null,
                "whatsappNumber": null,
                "facebook": null,
                "twitter": null,
                "linkedin": null,
                "instagram": null,
                "snapchat": null,
                "youtube": null,
                "tiktok": null
            },
        });
        return this.shopRepository.save(shop);
    }
    async findAll(vendorId, onlyActive = false) {
        const query = this.shopRepository
            .createQueryBuilder('shop')
            .leftJoinAndSelect('shop.user', 'user')
            .leftJoinAndSelect('shop.services', 'services')
            .leftJoinAndSelect('shop.contact', 'contact')
            .leftJoinAndSelect('shop.category', 'category')
            .leftJoinAndMapMany('shop.offers', shopoffer_entity_1.ShopOffer, 'offers', 'offers.shopId = shop.id');
        if (vendorId) {
            query.where('user.id = :vendorId', { vendorId: Number(vendorId) });
        }
        if (onlyActive) {
            query.andWhere('offers.status = :status', { status: 'active' });
        }
        query.distinct(true);
        return query.getMany();
    }
    async findOne(id) {
        const shop = await this.shopRepository.findOne({
            where: { id: id },
            relations: ["services", "contact"],
        });
        return shop;
    }
    async update(id, updateShopDto) {
        const shop = await this.shopRepository.findOne({
            where: { id },
            relations: ["services"],
        });
        if (!shop) {
            throw new common_1.NotFoundException(`Shop with ID ${id} not found`);
        }
        const { services, ...shopData } = updateShopDto;
        Object.assign(shop, shopData);
        if (services) {
            const updatedServices = [];
            for (const serviceDto of services) {
                if (serviceDto.id) {
                    const existingService = await this.serviceRepository.findOne({
                        where: { id: serviceDto.id },
                    });
                    if (!existingService) {
                        throw new common_1.NotFoundException(`Service with ID ${serviceDto.id} not found`);
                    }
                    Object.assign(existingService, serviceDto);
                    updatedServices.push(existingService);
                }
                else {
                    const newService = this.serviceRepository.create(serviceDto);
                    updatedServices.push(newService);
                }
            }
            shop.services = updatedServices;
        }
        await this.shopRepository.save(shop);
        return this.findOne(id);
    }
    async remove(id) {
        const result = await this.shopRepository.delete(id);
        if (result.affected === 0) {
            throw new common_1.NotFoundException(`Shop with ID ${id} not found`);
        }
    }
    async updateStatus(id, status) {
        const shop = await this.shopRepository.findOne({ where: { id } });
        if (!shop) {
            throw new common_1.NotFoundException(`Shop with id ${id} not found`);
        }
        shop.status = status.status;
        return await this.shopRepository.save(shop);
    }
};
exports.AdminShopService = AdminShopService;
exports.AdminShopService = AdminShopService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_2.InjectRepository)(shop_entity_1.Shop)),
    __param(1, (0, typeorm_2.InjectRepository)(service_entity_1.Service)),
    __param(2, (0, typeorm_2.InjectRepository)(user_entity_1.User)),
    __metadata("design:paramtypes", [typeorm_1.Repository,
        typeorm_1.Repository,
        typeorm_1.Repository])
], AdminShopService);
//# sourceMappingURL=admin.shop.service.js.map