package service

import (
	"apskel-pos-be/internal/appcontext"
	"apskel-pos-be/internal/constants"
	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/processor"
	"apskel-pos-be/internal/transformer"
	"context"

	"github.com/google/uuid"
)

type TableServiceImpl struct {
	tableProcessor   *processor.TableProcessor
	tableTransformer *transformer.TableTransformer
}

func NewTableService(tableProcessor *processor.TableProcessor, tableTransformer *transformer.TableTransformer) *TableServiceImpl {
	return &TableServiceImpl{
		tableProcessor:   tableProcessor,
		tableTransformer: tableTransformer,
	}
}

func (s *TableServiceImpl) CreateTable(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.CreateTableRequest) *contract.Response {
	modelReq := transformer.CreateTableRequestToModel(apctx, req)

	response, err := s.tableProcessor.Create(ctx, modelReq, apctx.OrganizationID)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractResponse := s.tableTransformer.ToContract(*response)
	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) UpdateTable(ctx context.Context, id uuid.UUID, req *contract.UpdateTableRequest) *contract.Response {
	modelReq := transformer.UpdateTableRequestToModel(req)

	response, err := s.tableProcessor.Update(ctx, id, modelReq)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractResponse := s.tableTransformer.ToContract(*response)
	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) DeleteTable(ctx context.Context, id uuid.UUID) *contract.Response {
	err := s.tableProcessor.Delete(ctx, id)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	return contract.BuildSuccessResponse(map[string]interface{}{
		"message": "Table deleted successfully",
	})
}

func (s *TableServiceImpl) GetTableByID(ctx context.Context, id uuid.UUID) *contract.Response {
	response, err := s.tableProcessor.GetByID(ctx, id)
	if err != nil {
		errorResp := contract.NewResponseError(constants.NotFoundErrorCode, constants.TableEntity, "Table not found")
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractResponse := s.tableTransformer.ToContract(*response)
	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) ListTables(ctx context.Context, req *contract.ListTablesQuery) *contract.Response {
	modelReq := transformer.ListTablesQueryToModel(req)

	response, err := s.tableProcessor.List(ctx, modelReq)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractTables := make([]contract.TableResponse, len(response.Tables))
	for i, table := range response.Tables {
		contractTables[i] = *s.tableTransformer.ToContract(table)
	}

	contractResponse := &contract.ListTablesResponse{
		Tables:     contractTables,
		TotalCount: response.TotalCount,
		Page:       response.Page,
		Limit:      response.Limit,
		TotalPages: response.TotalPages,
	}

	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) OccupyTable(ctx context.Context, tableID uuid.UUID, req *contract.OccupyTableRequest) *contract.Response {
	modelReq := transformer.OccupyTableRequestToModel(req)

	response, err := s.tableProcessor.OccupyTable(ctx, tableID, modelReq)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractResponse := s.tableTransformer.ToContract(*response)
	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) ReleaseTable(ctx context.Context, tableID uuid.UUID, req *contract.ReleaseTableRequest) *contract.Response {
	modelReq := transformer.ReleaseTableRequestToModel(req)

	response, err := s.tableProcessor.ReleaseTable(ctx, tableID, modelReq)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	contractResponse := s.tableTransformer.ToContract(*response)
	return contract.BuildSuccessResponse(contractResponse)
}

func (s *TableServiceImpl) GetAvailableTables(ctx context.Context, outletID uuid.UUID) *contract.Response {
	tables, err := s.tableProcessor.GetAvailableTables(ctx, outletID)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	responses := make([]contract.TableResponse, len(tables))
	for i, table := range tables {
		responses[i] = *s.tableTransformer.ToContract(table)
	}

	return contract.BuildSuccessResponse(responses)
}

func (s *TableServiceImpl) GetOccupiedTables(ctx context.Context, outletID uuid.UUID) *contract.Response {
	tables, err := s.tableProcessor.GetOccupiedTables(ctx, outletID)
	if err != nil {
		errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.TableEntity, err.Error())
		return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
	}

	responses := make([]contract.TableResponse, len(tables))
	for i, table := range tables {
		responses[i] = *s.tableTransformer.ToContract(table)
	}

	return contract.BuildSuccessResponse(responses)
}
