import axios from 'axios';
import env from '#start/env';
export async function validateCompanyHouseNumber(companyNumber) {
    try {
        let formattedNumber = companyNumber.trim().toUpperCase();
        if (/^\d+$/.test(formattedNumber)) {
            formattedNumber = formattedNumber.padStart(8, '0');
        }
        const apiKey = env.get('COMPANIES_HOUSE_API_KEY');
        if (!apiKey) {
            throw new Error('COMPANIES_HOUSE_API_KEY is missing in your environment configuration.');
        }
        const token = Buffer.from(`${apiKey}:`).toString('base64');
        const url = `https://api.company-information.service.gov.uk/company/${formattedNumber}`;
        const response = await axios.get(url, {
            headers: {
                'Authorization': `Basic ${token}`,
                'Accept': 'application/json',
            },
        });
        const company = response.data;
        return {
            isValid: true,
            company: {
                name: company.company_name,
                number: company.company_number,
                status: company.company_status,
                type: company.type,
                dateOfCreation: company.date_of_creation,
                address: company.registered_office_address,
            },
        };
    }
    catch (error) {
        if (axios.isAxiosError(error) && error.response?.status === 404) {
            return {
                isValid: false,
                message: 'The company number provided is invalid or does not exist.',
            };
        }
        console.error('[COMPANIES HOUSE API ERROR]:', error);
        throw new Error('Failed to verify company number with Companies House. Please try again later.');
    }
}
export async function validateCompanyReferenceNumber(firmReferenceNumber) {
    try {
        let formattedNumber = firmReferenceNumber.trim().toUpperCase();
        const apiEmail = env.get('FRN_API_EMAIL');
        const apiKey = env.get('FRN_API_KEY');
        if (!apiKey) {
            throw new Error('API key is missing in your environment configuration.');
        }
        if (!apiEmail) {
            throw new Error('API email is missing in your environment configuration.');
        }
        const url = `https://register.fca.org.uk/services/V0.1/Firm/${formattedNumber}`;
        const response = await axios.get(url, {
            headers: {
                'Accept': 'application/json',
                'X-Auth-Email': apiEmail,
                'X-Auth-Key': apiKey,
            },
        });
        const company = response.data;
        return {
            isValid: true,
            company: company.Data,
            name: company.Data?.[0]?.['Organisation Name'] ?? 'Name not found',
            message: company?.Message,
            status: company?.Status,
        };
    }
    catch (error) {
        if (axios.isAxiosError(error) && error.response?.status === 404) {
            return {
                isValid: false,
                message: 'The company number provided is invalid or does not exist.',
            };
        }
        console.error('[FRN API ERROR]:', error);
        throw new Error('Failed to verify company reference number. Please try again later.');
    }
}
//# sourceMappingURL=company_validation_service.js.map