package service

import (
	"context"

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

	"github.com/google/uuid"
)

type AccountService interface {
	contract.AccountContract
}

type AccountServiceImpl struct {
	processor processor.AccountProcessor
}

func NewAccountService(processor processor.AccountProcessor) AccountService {
	return &AccountServiceImpl{
		processor: processor,
	}
}

func (s *AccountServiceImpl) CreateAccount(ctx context.Context, req *contract.CreateAccountRequest) (*contract.AccountResponse, error) {
	modelReq := mappers.ContractToModelCreateAccountRequest(req)
	modelResp, err := s.processor.CreateAccount(ctx, modelReq)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractAccountResponse(modelResp), nil
}

func (s *AccountServiceImpl) GetAccountByID(ctx context.Context, id uuid.UUID) (*contract.AccountResponse, error) {
	modelResp, err := s.processor.GetAccountByID(ctx, id)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractAccountResponse(modelResp), nil
}

func (s *AccountServiceImpl) UpdateAccount(ctx context.Context, id uuid.UUID, req *contract.UpdateAccountRequest) (*contract.AccountResponse, error) {
	modelReq := mappers.ContractToModelUpdateAccountRequest(req)
	modelResp, err := s.processor.UpdateAccount(ctx, id, modelReq)
	if err != nil {
		return nil, err
	}
	return mappers.ModelToContractAccountResponse(modelResp), nil
}

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

func (s *AccountServiceImpl) ListAccounts(ctx context.Context, req *contract.ListAccountsRequest) ([]contract.AccountResponse, int, error) {
	modelReq := mappers.ContractToModelListAccountsRequest(req)
	modelResp, total, err := s.processor.ListAccounts(ctx, modelReq)
	if err != nil {
		return nil, 0, err
	}
	
	contractResp := make([]contract.AccountResponse, len(modelResp))
	for i, resp := range modelResp {
		contractResp[i] = *mappers.ModelToContractAccountResponse(&resp)
	}
	
	return contractResp, total, nil
}

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

func (s *AccountServiceImpl) GetAccountsByChartOfAccount(ctx context.Context, chartOfAccountID uuid.UUID) ([]contract.AccountResponse, error) {
	modelResp, err := s.processor.GetAccountsByChartOfAccount(ctx, chartOfAccountID)
	if err != nil {
		return nil, err
	}
	
	contractResp := make([]contract.AccountResponse, len(modelResp))
	for i, resp := range modelResp {
		contractResp[i] = *mappers.ModelToContractAccountResponse(&resp)
	}
	
	return contractResp, nil
}

func (s *AccountServiceImpl) UpdateAccountBalance(ctx context.Context, id uuid.UUID, req *contract.UpdateAccountBalanceRequest) error {
	return s.processor.UpdateAccountBalance(ctx, id, req.Amount)
}

func (s *AccountServiceImpl) GetAccountBalance(ctx context.Context, id uuid.UUID) (float64, error) {
	return s.processor.GetAccountBalance(ctx, id)
}
