import { dev } from '$app/environment'
export const server = dev ? 'http://localhost:5173' : 'https://coturnix.fr'

export type SimulationCommunaute = {
    puissance: number
    economies?: number
    autoconso: number
    autoprod: number
    frais_divers: number
    prix: number
    apport: number
    interet: number
    echeances: number
    conso_moyenne_foyer: number
    productible: number
    degradation: number
    inflation: number
    ratio: number
    enedis: number
    tarif_cre: {
        limites: number[]
        prix: number[]
        inflation: number
    }
    cout_elec: number
    inflation_elec: number
    maintenance: number
    turpe: number
    turpe_injection: number
    provision_onduleurs: number
    assurance: number
    divers: number
    ratio_dc_ac: number
    duree_amortissement: number
    taux_is: number
    taux_dsra: number
}

export function newSimulationCommunaute(): SimulationCommunaute {
    return {
        puissance: 0,
        autoconso: 90,
        autoprod: 10,
        frais_divers: 1000,
        prix: 0.13,
        apport: 10000,
        interet: 4.5,
        echeances: 96,
        conso_moyenne_foyer: 2500, //Kwh/an
        productible: 1100,
        degradation: 0.6,
        inflation: 1,
        ratio: 1.1, // €/Wc
        enedis: 1500,
        tarif_cre: {
            limites: [0, 9, 100],
            prix: [0.1301, 0.781, 0.1141],
            inflation: 1,
        },
        cout_elec: 0.24, // €
        inflation_elec: 3, // %
        maintenance: 7, // €/kWc
        turpe: 16, // €/kWc
        turpe_injection: 0.0278,
        provision_onduleurs: 0.1, // €/Wc
        assurance: 1, // % des travaux estimé entre 0.5% et 0.8%
        divers: 12,
        ratio_dc_ac: 0.8,
        duree_amortissement: 10, // durée fiscale en années
        taux_is: 25,
        taux_dsra: 1.7, // %
    }
}

export type SaveFunction = (
    e: Event,
    nom: string,
    simulation: SimulationCommunaute,
    prms: Prm[],
    profils: Profil[],
    communaute?: Communaute
) => Promise<void>

export type Rapport = {
    id: string
    nom: string
    simulation: SimulationCommunaute
    communaute?: Communaute
    production?: number[]
    charge?: Record<string, number[]>
    interets_annuel: number[]
    remboursement_annuel: number[]
}

export type RapportFunction = (r: Rapport) => Promise<void>

export type ListProjets = { ListProjets: null }
export type ListProjetsResp = {
    id: string
    nom: string
    puissance: number
    t: number
}[]

export type CreateProjet = {
    CreateProjet: {
        nom?: string
        porteur?: string
        tel?: string
        adresse?: string
        centrales: Centrale[]
        simulation?: SimulationCommunaute
        prms: Prm[]
    }
}

export type Centrale = {
    position?: { lat: number; lng: number }
    azimuth?: number
    inclinaison?: number
}

export type CreateProjetResp = null | string

export type SaveProjet = {
    SaveProjet: {
        id: string
        nom?: string
        porteur?: string
        tel?: string
        adresse?: string
        communaute?: Communaute
        simulation?: SimulationCommunaute
        prms: Prm[]
        profils: Profil[]
        centrales: Centrale[]
    }
}

export type Prm = {
    prm: number | null
    nom: string
    prenom: string
    siret?: string
    serie?: string
    codeinsee?: string
    numerorue?: string
    pro?: boolean
    releve?: string
    consentement?: boolean
}

export type Profil = {
    type: string
    conso: number
}

export type GetProjet = { GetProjet: string }
export type GetProjetResp = null | {
    id: string
    nom?: string
    porteur?: string
    tel?: string
    adresse?: string
    communaute?: Communaute
    simulation?: SimulationCommunaute
    img?: string
    prms: Prm[]
    profils: Profil[]
    expert: boolean
}

export type DelProjet = {
    DelProjet: {
        id: string
    }
}

export type Cmd =
    | ListProjets
    | CreateProjet
    | SaveProjet
    | DelProjet
    | GetProjet

export type LatLng = { lat: number; lng: number }
export type Derogation = 'non' | '10' | '20'
export type Compteur = {
    lat: number
    lng: number
    puissance: number
    azimuth: number
    inclinaison: number
}
export type Communaute = {
    lat: number
    lng: number
    derogation: Derogation
    compteurs?: Compteur[]
}

export function profils(expert: boolean) {
    const profils_: Record<string, { profils: string[] }> = {
        'Résidentiel ≤6kVA': {
            profils: ['RES1-P1'],
        },

        'Résidentiel 6>kVA≤36': {
            profils: ['RES11-P1'],
        },

        'Résidentiel ≤36kVA HPHC': {
            profils: ['RES2'],
        },

        'Professionel ≤36kVA': {
            profils: ['PRO1-P1'],
        },

        'Professionnel ≤36kVA HWE': {
            profils: ['PRO1WE'],
        },

        'Professionnel ≤36kVA HPHC': {
            profils: ['PRO2'],
        },

        'Entreprise 36<kVA<250 HPHC ETE-HIV': {
            profils: ['ENT1'],
        },

        /*
           "Production hydraulique":{
           "profils":[ "PRD1-P1" ]
           },

           "Production cogénération":{
           "profils":[ "PRD2-P1" ]
           },

           "Production photovoltaïque":{
           "profils":[ "PRD3-P1" ]
           },
         */
    }
    if (expert) {
        for (const p of [
            'ENT1-P1',
            'ENT1-P2',
            'ENT1-P3',
            'ENT1-P4',
            'ENT2-P1',
            'ENT2-P2',
            'ENT2-P3',
            'ENT2-P4',
            'ENT3-P1',
            'ENT3-P2',
            'ENT3-P3',
            'ENT3-P4',
            'ENT3-P5',
            'ENT4-P1',
            'ENT4-P2',
            'ENT4-P3',
            'ENT4-P4',
            'ENT5-P1',
            'ENT5-P2',
            'ENT5-P3',
            'ENT5-P4',
            'ENT5-P5',
            'ENT5-P6',
            'ENT5-P7',
            'ENT5-P8',
            'ENT6-P1',
            'ENT6-P2',
            'ENT6-P3',
            'ENT6-P4',
            'ENT6-P5',
            'ENT6-P6',
            'ENT7-P1',
            'ENT7-P2',
            'ENT7-P3',
            'ENT7-P4',
            'ENT7-P5',
            'PRD1-P1',
            'PRD2-P1',
            'PRD3-P1',
            'PRD4-P1',
            'PRO1-P1',
            'PRO1WE-P1',
            'PRO1WE-P2',
            'PRO22WE-P1',
            'PRO22WE-P2',
            'PRO22WE-P3',
            'PRO22WE-P4',
            'PRO2-P1',
            'PRO2-P2',
            'PRO3-P1',
            'PRO3-P2',
            'PRO3-P3',
            'PRO3-P4',
            'PRO3-P5',
            'PRO3-P6',
            'PRO4-P1',
            'PRO4-P2',
            'PRO5-P1',
            'PRO6-P1',
            'PRO6-P2',
            'PRO6-P3',
            'PRO6-P4',
            'RES11-P1',
            'RES11WE-P1',
            'RES11WE-P2',
            'RES1-P1',
            'RES1WE-P1',
            'RES1WE-P2',
            'RES22WE-P1',
            'RES22WE-P2',
            'RES22WE-P3',
            'RES22WE-P4',
            'RES2-P1',
            'RES2-P2',
            'RES2WE-P1',
            'RES2WE-P2',
            'RES2WE-P3',
            'RES3-P1',
            'RES3-P2',
            'RES3-P3',
            'RES3-P4',
            'RES3-P5',
            'RES3-P6',
            'RES4-P1',
            'RES4-P2',
            'RES5-P1',
            'RES5-P2',
            'RES5-P3',
            'RES5-P4',
        ]) {
            profils_[p] = {
                profils: [p],
            }
        }
    }
    return profils_
}