"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); }
};
var DropdownOptionsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropdownOptionsService = void 0;
const common_1 = require("@nestjs/common");
const mongoose_1 = require("@nestjs/mongoose");
const mongoose_2 = require("mongoose");
const dropdown_option_schema_1 = require("./dropdown-option.schema");
const VALID_CATEGORIES = [
    'religion',
    'caste',
    'medium',
    'nationality',
    'guardianRelation',
    'bloodGroup',
];
const DEFAULTS = {
    religion: ['Hindu', 'Muslim', 'Christian', 'Sikh', 'Buddhist', 'Jain', 'Parsi', 'Jewish', 'Other'],
    caste: ['SC', 'ST', 'OBC', 'General'],
    medium: ['English', 'Bengali', 'Hindi', 'Other'],
    nationality: ['Indian', 'Bangladeshi', 'Nepali', 'Sri Lankan', 'Other'],
    guardianRelation: ['Father', 'Mother', 'Uncle', 'Aunt', 'Grandparent', 'Other'],
    bloodGroup: ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-'],
};
let DropdownOptionsService = DropdownOptionsService_1 = class DropdownOptionsService {
    model;
    logger = new common_1.Logger(DropdownOptionsService_1.name);
    constructor(model) {
        this.model = model;
    }
    async onModuleInit() {
        await this.seedDefaults();
    }
    async seedDefaults() {
        for (const [category, values] of Object.entries(DEFAULTS)) {
            for (const value of values) {
                const exists = await this.model
                    .findOne({ category, value })
                    .collation({ locale: 'en', strength: 2 })
                    .lean();
                if (!exists) {
                    await this.model.create({ category, value, isDefault: true });
                }
            }
        }
        this.logger.log('Dropdown option defaults seeded');
    }
    async findByCategory(category) {
        return this.model
            .find({ category })
            .sort({ isDefault: -1, value: 1 })
            .lean();
    }
    async create(category, value) {
        if (!VALID_CATEGORIES.includes(category)) {
            throw new common_1.BadRequestException(`Invalid category: ${category}`);
        }
        const trimmed = value.trim();
        if (!trimmed) {
            throw new common_1.BadRequestException('Value is required');
        }
        const existing = await this.model
            .findOne({ category, value: trimmed })
            .collation({ locale: 'en', strength: 2 })
            .lean();
        if (existing) {
            throw new common_1.ConflictException(`"${trimmed}" already exists in ${category}`);
        }
        const doc = await this.model.create({ category, value: trimmed, isDefault: false });
        return doc.toObject();
    }
};
exports.DropdownOptionsService = DropdownOptionsService;
exports.DropdownOptionsService = DropdownOptionsService = DropdownOptionsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, mongoose_1.InjectModel)(dropdown_option_schema_1.DropdownOption.name)),
    __metadata("design:paramtypes", [mongoose_2.Model])
], DropdownOptionsService);
//# sourceMappingURL=dropdown-options.service.js.map