package service

import (
	"context"

	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/mappers"
	"apskel-pos-be/internal/processor"

	"github.com/google/uuid"
)

type ChartOfAccountService interface {
	contract.ChartOfAccountContract
}

type ChartOfAccountServiceImpl struct {
	processor processor.ChartOfAccountProcessor
}

func NewChartOfAccountService(processor processor.ChartOfAccountProcessor) ChartOfAccountService {
	return &ChartOfAccountServiceImpl{
		processor: processor,
	}
}

func (s *ChartOfAccountServiceImpl) CreateChartOfAccount(ctx context.Context, req *contract.CreateChartOfAccountRequest) (*contract.ChartOfAccountResponse, error) {
	modelReq := mappers.ContractToModelCreateChartOfAccountRequest(req)
	modelResp, err := s.processor.CreateChartOfAccount(ctx, modelReq)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractChartOfAccountResponse(modelResp), nil
}

func (s *ChartOfAccountServiceImpl) GetChartOfAccountByID(ctx context.Context, id uuid.UUID) (*contract.ChartOfAccountResponse, error) {
	modelResp, err := s.processor.GetChartOfAccountByID(ctx, id)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractChartOfAccountResponse(modelResp), nil
}

func (s *ChartOfAccountServiceImpl) UpdateChartOfAccount(ctx context.Context, id uuid.UUID, req *contract.UpdateChartOfAccountRequest) (*contract.ChartOfAccountResponse, error) {
	modelReq := mappers.ContractToModelUpdateChartOfAccountRequest(req)
	modelResp, err := s.processor.UpdateChartOfAccount(ctx, id, modelReq)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractChartOfAccountResponse(modelResp), nil
}

func (s *ChartOfAccountServiceImpl) DeleteChartOfAccount(ctx context.Context, id uuid.UUID) error {
	return s.processor.DeleteChartOfAccount(ctx, id)
}

func (s *ChartOfAccountServiceImpl) ListChartOfAccounts(ctx context.Context, req *contract.ListChartOfAccountsRequest) ([]contract.ChartOfAccountResponse, int, error) {
	modelReq := mappers.ContractToModelListChartOfAccountsRequest(req)
	modelResp, total, err := s.processor.ListChartOfAccounts(ctx, modelReq)
	if err != nil {
		return nil, 0, err
	}
	
	contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
	for i, resp := range modelResp {
		contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
	}
	
	return contractResp, total, nil
}

func (s *ChartOfAccountServiceImpl) GetChartOfAccountsByOrganization(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID) ([]contract.ChartOfAccountResponse, error) {
	modelResp, err := s.processor.GetChartOfAccountsByOrganization(ctx, organizationID, outletID)
	if err != nil {
		return nil, err
	}
	
	contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
	for i, resp := range modelResp {
		contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
	}
	
	return contractResp, nil
}

func (s *ChartOfAccountServiceImpl) GetChartOfAccountsByType(ctx context.Context, organizationID uuid.UUID, chartOfAccountTypeID uuid.UUID, outletID *uuid.UUID) ([]contract.ChartOfAccountResponse, error) {
	modelResp, err := s.processor.GetChartOfAccountsByType(ctx, organizationID, chartOfAccountTypeID, outletID)
	if err != nil {
		return nil, err
	}
	
	contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
	for i, resp := range modelResp {
		contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
	}
	
	return contractResp, nil
}
