import { Injectable } from "@nestjs/common";
import { PrismaMariaDbService } from "prisma/mariadb/prisma-mariadb.service";
import { Result } from "src/common/errors/result-error.factory";
import { Account } from "src/customer-support/accounts/account.dto";


@Injectable()
export class AccountService {
    constructor(private readonly prismaMariaDb: PrismaMariaDbService) { }

    findAllAccounts = async (): Promise<Result<Account[], string>> => {
        const listOfAccounts: Account[] = [];

        const rawListOfAccounts = await this.prismaMariaDb.account.findMany({});
        if (!rawListOfAccounts) return { success: false, error: 'ACCOUNTS_NOT_FOUND' }
        try {

            for (const account of rawListOfAccounts) {
                const emailList: string[] = [
                    account.email ? account.email : '',
                    account.email_autre ? account.email_autre : '',
                ];

                const addressList: string[] = [
                    account.address1 ? account.address1 : '',
                    account.address2 ? account.address2 : '',
                    account.city ? account.city : '',
                    account.state ? account.state : '',
                    account.zip ? account.zip : '',
                    account.country_id.toString(),
                ];

                listOfAccounts.push({
                    id: Number(account.id),
                    customerId: account.customer_id ? account.customer_id : '',
                    language: account.language_id,
                    username: account.username ? account.username : '',
                    password: account.password ? account.password : '',
                    groupId: account.group_id ? account.group_id : 0,
                    status: account.status ? account.status : 0,
                    firstName: account.first_name ? account.first_name : '',
                    lastName: account.last_name ? account.last_name : '',
                    mandataire: account.mandataire ? account.mandataire : '',
                    title: account.title ? account.title : '',
                    email: emailList,
                    company: account.company ? account.company : '',
                    contact: account.contact,
                    address: addressList,
                    telHome: account.tel_home ? account.tel_home : '',
                    telOffice: account.tel_office ? account.tel_office : '',
                    telOffice_ext: account.tel_office_ext ? account.tel_office_ext : '',
                    cell: account.cell ? account.cell : '',
                    fax: account.fax ? account.fax : '',
                    landOwner: account.land_owner,
                    commercial: account.commercial,
                    vip: account.vip,
                    notes_client: account.notes_client ? account.notes_client : '',
                    terminateReason: account.terminate_reason ? account.terminate_reason : '',
                    terminateCie: account.terminate_cie ? account.terminate_cie : '',
                    terminateNote: account.terminate_note ? account.terminate_note : '',
                    terminateDate: account.terminate_date ? account.terminate_date : '',
                    mauvaisPayeur: account.mauvais_payeur,
                });
            }
            console.log(listOfAccounts);
            return { success: true, data: listOfAccounts };

        } catch (error) {
            return { success: false, error: error + " \n An error occured during retrieving the list of accounts" }
        }
    }

    findAccountById = async (accountId: number): Promise<Result<Account, string>> => {

        const rawAccount = await this.prismaMariaDb.account.findUnique({
            where: { id: accountId }
        });
        if (!rawAccount) return { success: false, error: 'ACCOUNT_NOT_FOUND' }

        const emailList: string[] = [
            rawAccount.email ? rawAccount.email : '',
            rawAccount.email_autre ? rawAccount.email_autre : '',
        ];

        const addressList: string[] = [
            rawAccount.address1 ? rawAccount.address1 : '',
            rawAccount.address2 ? rawAccount.address2 : '',
            rawAccount.city ? rawAccount.city : '',
            rawAccount.state ? rawAccount.state : '',
            rawAccount.zip ? rawAccount.zip : '',
            rawAccount.country_id.toString(),
        ];

        const account: Account = {
            id: Number(rawAccount.id),
            customerId: rawAccount.customer_id ? rawAccount.customer_id : '',
            language: rawAccount.language_id,
            username: rawAccount.username ? rawAccount.username : '',
            password: rawAccount.password ? rawAccount.password : '',
            groupId: rawAccount.group_id ? rawAccount.group_id : 0,
            status: rawAccount.status ? rawAccount.status : 0,
            firstName: rawAccount.first_name ? rawAccount.first_name : '',
            lastName: rawAccount.last_name ? rawAccount.last_name : '',
            mandataire: rawAccount.mandataire ? rawAccount.mandataire : '',
            title: rawAccount.title ? rawAccount.title : '',
            email: emailList,
            company: rawAccount.company ? rawAccount.company : '',
            contact: rawAccount.contact,
            address: addressList,
            telHome: rawAccount.tel_home ? rawAccount.tel_home : '',
            telOffice: rawAccount.tel_office ? rawAccount.tel_office : '',
            telOffice_ext: rawAccount.tel_office_ext ? rawAccount.tel_office_ext : '',
            cell: rawAccount.cell ? rawAccount.cell : '',
            fax: rawAccount.fax ? rawAccount.fax : '',
            landOwner: rawAccount.land_owner,
            commercial: rawAccount.commercial,
            vip: rawAccount.vip,
            notes_client: rawAccount.notes_client ? rawAccount.notes_client : '',
            terminateReason: rawAccount.terminate_reason ? rawAccount.terminate_reason : '',
            terminateCie: rawAccount.terminate_cie ? rawAccount.terminate_cie : '',
            terminateNote: rawAccount.terminate_note ? rawAccount.terminate_note : '',
            terminateDate: rawAccount.terminate_date ? rawAccount.terminate_date : '',
            mauvaisPayeur: rawAccount.mauvais_payeur,
        }

        return { success: true, data: account };
    }
}
