/**
 * GST Utility for state validation and tax calculations
 * Contains state codes and helper functions for determining GST components
 */

export const STATE_CODES: Record<string, string> = {
    '01': 'Jammu and Kashmir',
    '02': 'Himachal Pradesh',
    '03': 'Punjab',
    '04': 'Chandigarh',
    '05': 'Uttarakhand',
    '06': 'Haryana',
    '07': 'Delhi',
    '08': 'Rajasthan',
    '09': 'Uttar Pradesh',
    '10': 'Bihar',
    '11': 'Sikkim',
    '12': 'Arunachal Pradesh',
    '13': 'Nagaland',
    '14': 'Manipur',
    '15': 'Mizoram',
    '16': 'Tripura',
    '17': 'Meghalaya',
    '18': 'Assam',
    '19': 'West Bengal',
    '20': 'Jharkhand',
    '21': 'Odisha',
    '22': 'Chhattisgarh',
    '23': 'Madhya Pradesh',
    '24': 'Gujarat',
    '25': 'Daman and Diu',
    '26': 'Dadra and Nagar Haveli',
    '27': 'Maharashtra',
    '29': 'Karnataka',
    '30': 'Goa',
    '31': 'Lakshadweep Islands',
    '32': 'Kerala',
    '33': 'Tamil Nadu',
    '34': 'Pondicherry',
    '35': 'Andaman and Nicobar',
    '36': 'Telangana',
    '37': 'Andhra Pradesh',
    '38': 'Ladakh',
    '97': 'Others',
};

/**
 * Royal Enfield State Code (Tamil Nadu)
 */
export const COMPANY_STATE_CODE = '33';

/**
 * State codes that use UTGST instead of SGST
 * Andaman and Nicobar Islands, Chandigarh, Dadra and Nagar Haveli and Daman and Diu, Ladakh, Lakshadweep
 */
export const UT_STATE_CODES = new Set(['04', '25', '26', '31', '35', '38']);

/**
 * Extracts state code from GSTIN
 * @param gstin The 15-digit GSTIN string
 * @returns 2-digit state code or null
 */
export const getStateCodeFromGSTIN = (gstin: string | undefined | null): string | null => {
    if (!gstin || gstin.length < 2) return null;
    const stateCode = gstin.substring(0, 2);
    return STATE_CODES[stateCode] ? stateCode : null;
};

/**
 * Checks if a state code corresponds to a Union Territory (requiring UTGST)
 * @param stateCode 2-digit state code
 * @returns boolean
 */
export const isUnionTerritory = (stateCode: string | undefined | null): boolean => {
    if (!stateCode) return false;
    return UT_STATE_CODES.has(stateCode);
};

/**
 * Determines if a transaction is Inter-state (IGST) or Intra-state (CGST+SGST/UTGST)
 * @param dealerStateCode 2-digit state code of the dealer
 * @returns true if IGST, false if CGST+SGST/UTGST
 */
export const isInterState = (dealerStateCode: string | undefined | null): boolean => {
    if (!dealerStateCode) return false; // Default to intra-state if unknown
    return dealerStateCode !== COMPANY_STATE_CODE;
};

/**
 * Gets the tax components for a given dealer state
 * @param dealerStateCode 2-digit state code of the dealer
 * @returns Object indicating which tax components are active
 */
export const getActiveTaxComponents = (dealerStateCode: string | undefined | null) => {
    if (!dealerStateCode) {
        return {
            isIGST: false,
            isCGST: true,
            isSGST: true,
            isUTGST: false,
        };
    }

    const isInter = isInterState(dealerStateCode);

    if (isInter) {
        return {
            isIGST: true,
            isCGST: false,
            isSGST: false,
            isUTGST: false,
        };
    }

    const isUT = isUnionTerritory(dealerStateCode);
    return {
        isIGST: false,
        isCGST: true,
        isSGST: !isUT,
        isUTGST: isUT,
    };
};
