"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
const crypto_1 = require("crypto");
const promise_1 = __importDefault(require("mysql2/promise"));
const bcryptjs_1 = __importDefault(require("bcryptjs"));
async function main() {
    const databaseUrl = process.env.DATABASE_URL;
    let connection;
    if (databaseUrl) {
        connection = await promise_1.default.createConnection(databaseUrl);
    }
    else {
        connection = await promise_1.default.createConnection({
            host: process.env.DB_HOST || 'localhost',
            port: parseInt(process.env.DB_PORT || '') || 3306,
            user: process.env.DB_USERNAME || 'root',
            password: process.env.DB_PASSWORD || '',
            database: process.env.DB_DATABASE || 'tracker',
        });
    }
    try {
        console.log('Starting seed script...');
        const ownerId = '640bed22-2b5f-4d86-b42c-b20c487954e9';
        const [owners] = await connection.query('SELECT id FROM owner WHERE id = ?', [ownerId]);
        const hashedAdminPassword = bcryptjs_1.default.hashSync('Admin@123456', 10);
        const defaultImage = JSON.stringify({
            file_name: 'uploads/common/profile.webp',
            original_name: 'profile.webp',
            file_size: 8192,
            mime_type: 'image/webp'
        });
        if (owners.length === 0) {
            console.log('Inserting owner Hridoy...');
            await connection.query(`INSERT INTO owner (id, name, email, username, password, address, phone, image, role, otp, status, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, [
                ownerId,
                'Hridoy',
                'hridoyhaque.info@gmail.com',
                'ridoy',
                hashedAdminPassword,
                'Dhaka, Bangladesh',
                '+880123456789',
                defaultImage,
                'owner',
                null,
                'active'
            ]);
        }
        else {
            console.log('Updating owner Hridoy...');
            await connection.query(`UPDATE owner SET password = ?, status = ? WHERE id = ?`, [hashedAdminPassword, 'active', ownerId]);
        }
        const [roles] = await connection.query('SELECT id, slug FROM access_roles WHERE owner_id = ?', [ownerId]);
        let memberRoleId = '';
        const memberRole = roles.find((r) => r.slug === 'member');
        if (memberRole) {
            memberRoleId = memberRole.id;
        }
        const [companies] = await connection.query('SELECT id FROM company WHERE owner_id = ?', [ownerId]);
        if (companies.length === 0) {
            console.log('Seeding company...');
            await connection.query(`INSERT INTO company (id, name, company_code, email, phone, address, website, logo, description, founded_at, industry, status, owner_id, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, [
                (0, crypto_1.randomUUID)(),
                'Wincasoft Solutions',
                'WIN',
                'info@wincasoft.com',
                '+880123456789',
                'Dhaka, Bangladesh',
                'https://wincasoft.com',
                defaultImage,
                'Software Development Company',
                new Date(),
                'Information Technology',
                'active',
                ownerId
            ]);
        }
        const [schedules] = await connection.query('SELECT id FROM working_schedule WHERE owner_id = ?', [ownerId]);
        if (schedules.length === 0) {
            console.log('Seeding working schedule...');
            await connection.query(`INSERT INTO working_schedule (id, working_days, working_days_per_week, working_hours_per_day, office_start_time, office_end_time, half_day_min_time, half_day_max_time, timezone, monthly_correction_limit, owner_id, status, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, [
                (0, crypto_1.randomUUID)(),
                JSON.stringify(['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']),
                6,
                8,
                '2026-01-01 09:00:00',
                '2026-01-01 17:00:00',
                4,
                6,
                'Asia/Dhaka',
                2,
                ownerId,
                'active'
            ]);
        }
        const [leavePolicies] = await connection.query('SELECT id FROM leave_policy WHERE owner_id = ?', [ownerId]);
        if (leavePolicies.length === 0) {
            console.log('Seeding leave policy...');
            await connection.query(`INSERT INTO leave_policy (id, annual_leave_days, sick_leave_days, casual_leave_days, maternity_leave_days, paternity_leave_days, personal_leave_days, compensatory_leave_days, unpaid_leave_days, owner_id, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, [
                (0, crypto_1.randomUUID)(),
                15,
                10,
                10,
                90,
                10,
                5,
                0,
                15,
                ownerId
            ]);
        }
        const getOrInsert = async (table, nameField, codeField, name, code, extraCols = '', extraVals = []) => {
            const queryStr = codeField
                ? `SELECT id FROM \`${table}\` WHERE owner_id = ? AND \`${codeField}\` = ?`
                : `SELECT id FROM \`${table}\` WHERE owner_id = ? AND \`${nameField}\` = ?`;
            const params = codeField ? [ownerId, code] : [ownerId, name];
            const [rows] = await connection.query(queryStr, params);
            if (rows.length > 0)
                return rows[0].id;
            const id = (0, crypto_1.randomUUID)();
            const insertQuery = codeField
                ? `INSERT INTO \`${table}\` (id, owner_id, \`${nameField}\`, \`${codeField}\` ${extraCols}, created_at, updated_at) VALUES (?, ?, ?, ? ${extraVals.map(() => '?').map(v => ', ' + v).join('')}, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`
                : `INSERT INTO \`${table}\` (id, owner_id, \`${nameField}\` ${extraCols}, created_at, updated_at) VALUES (?, ?, ? ${extraVals.map(() => '?').map(v => ', ' + v).join('')}, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`;
            const insertParams = codeField ? [id, ownerId, name, code, ...extraVals] : [id, ownerId, name, ...extraVals];
            await connection.query(insertQuery, insertParams);
            console.log(`Seeded ${table}: ${name}`);
            return id;
        };
        const engDeptId = await getOrInsert('departments', 'name', 'department_code', 'Engineering', 'ENG');
        const hrDeptId = await getOrInsert('departments', 'name', 'department_code', 'Human Resources', 'HR');
        const qaDeptId = await getOrInsert('departments', 'name', 'department_code', 'Quality Assurance', 'QA');
        const sseDesigId = await getOrInsert('designations', 'name', 'designation_code', 'Senior Software Engineer', 'SSE');
        const qaDesigId = await getOrInsert('designations', 'name', 'designation_code', 'QA Engineer', 'QAE');
        const hrmDesigId = await getOrInsert('designations', 'name', 'designation_code', 'HR Manager', 'HRM');
        const dhkLocId = await getOrInsert('locations', 'name', 'location_code', 'Dhaka Head Office', 'DHK_HQ');
        const remoteLocId = await getOrInsert('locations', 'name', 'location_code', 'Remote', 'REMOTE');
        const ftEmpId = await getOrInsert('employement_types', 'name', null, 'Full-Time', null, ', description', ['Regular full-time employee']);
        const ptEmpId = await getOrInsert('employement_types', 'name', null, 'Part-Time', null, ', description', ['Regular part-time employee']);
        const linkedinSourceId = await getOrInsert('hiring_sources', 'name', null, 'LinkedIn', null, ', description', ['LinkedIn job board']);
        const referralSourceId = await getOrInsert('hiring_sources', 'name', null, 'Employee Referral', null, ', description', ['Referral']);
        if (!memberRoleId) {
            const [mRole] = await connection.query('SELECT id FROM access_roles WHERE owner_id = ? AND slug = ?', [ownerId, 'member']);
            if (mRole.length > 0)
                memberRoleId = mRole[0].id;
        }
        const hashedMemberPassword = bcryptjs_1.default.hashSync('User@123456', 10);
        const membersToSeed = [
            {
                email: 'alice@wincasoft.com',
                work_email: 'alice.work@wincasoft.com',
                employee_id: 'WIN-001',
                first_name: 'Alice',
                last_name: 'Smith',
                dept_id: engDeptId,
                desig_id: sseDesigId,
                loc_id: dhkLocId,
                emp_type_id: ftEmpId,
                source_id: linkedinSourceId,
            },
            {
                email: 'bob@wincasoft.com',
                work_email: 'bob.work@wincasoft.com',
                employee_id: 'WIN-002',
                first_name: 'Bob',
                last_name: 'Jones',
                dept_id: qaDeptId,
                desig_id: qaDesigId,
                loc_id: remoteLocId,
                emp_type_id: ptEmpId,
                source_id: referralSourceId,
            },
            {
                email: 'charlie@wincasoft.com',
                work_email: 'charlie.work@wincasoft.com',
                employee_id: 'WIN-003',
                first_name: 'Charlie',
                last_name: 'HR',
                dept_id: hrDeptId,
                desig_id: hrmDesigId,
                loc_id: dhkLocId,
                emp_type_id: ftEmpId,
                source_id: linkedinSourceId,
            }
        ];
        for (const m of membersToSeed) {
            const [exist] = await connection.query('SELECT id FROM members WHERE owner_id = ? AND email = ?', [ownerId, m.email]);
            if (exist.length === 0) {
                console.log(`Seeding member ${m.first_name} ${m.last_name}...`);
                await connection.query(`INSERT INTO members (
            id, email, work_email, employee_id, image, nickname, first_name, last_name, password,
            about, expertise, work_phone, phone, father_name, mother_name,
            emergency_contact_name, emergency_contact_phone, emergency_contact_relationship,
            role, access_role_id, marital_status,
            present_address_line_one, present_address_line_two, present_address_city, present_address_country, present_address_state, present_address_zip_code,
            permanent_address_line_one, permanent_address_line_two, permanent_address_city, permanent_address_country, permanent_address_state, permanent_address_zip_code,
            gender, date_of_birth, date_of_joining,
            department_id, designation_id, location_id, employment_type_id, hiring_source_id,
            reporting_manager_id, status, owner_id, created_at, updated_at
          ) VALUES (
            ?, ?, ?, ?, ?, ?, ?, ?, ?,
            ?, ?, ?, ?, ?, ?,
            ?, ?, ?,
            ?, ?, ?,
            ?, ?, ?, ?, ?, ?,
            ?, ?, ?, ?, ?, ?,
            ?, ?, CURRENT_TIMESTAMP(3),
            ?, ?, ?, ?, ?,
            NULL, 'active', ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3)
          )`, [
                    (0, crypto_1.randomUUID)(),
                    m.email,
                    m.work_email,
                    m.employee_id,
                    defaultImage,
                    m.first_name,
                    m.first_name,
                    m.last_name,
                    hashedMemberPassword,
                    'Staff member',
                    'IT',
                    '+88011111111',
                    '+88022222222',
                    'Father Name',
                    'Mother Name',
                    'Emergency Name',
                    '+88033333333',
                    'Friend',
                    'member',
                    memberRoleId || null,
                    'single',
                    'Address 1',
                    'Address 2',
                    'Dhaka',
                    'Bangladesh',
                    'Dhaka',
                    '1200',
                    'Address 1',
                    'Address 2',
                    'Dhaka',
                    'Bangladesh',
                    'Dhaka',
                    '1200',
                    'male',
                    new Date('1995-01-01'),
                    m.dept_id,
                    m.desig_id,
                    m.loc_id,
                    m.emp_type_id,
                    m.source_id,
                    ownerId
                ]);
            }
        }
        console.log('Database seeding successfully completed.');
    }
    finally {
        await connection.end();
    }
}
main().catch((err) => {
    console.error('Seeding error:', err);
    process.exit(1);
});
//# sourceMappingURL=seed-dummy-data.js.map