package bed

import (
	"context"
	"service/pkg/errors"
)

type Service interface {
	GetBedList(ctx context.Context, kdPpk string, start, limit int) ([]BedData, error)
	CreateBed(ctx context.Context, kdPpk string, req BedData) error
	UpdateBed(ctx context.Context, kdPpk string, req BedData) error
	DeleteBed(ctx context.Context, kdPpk string, req BedDeletePayload) error
}

type service struct {
	repo Repository
}

func NewService(repo Repository) Service {
	return &service{repo: repo}
}

func (s *service) GetBedList(ctx context.Context, kdPpk string, start, limit int) ([]BedData, error) {
	if kdPpk == "" {
		return nil, errors.NewValidationError().Message("Kode PPK (Kode Faskes) wajib diisi").Build()
	}

	res, err := s.repo.Read(ctx, kdPpk, start, limit)
	if err != nil {
		return nil, errors.ExternalError().Message("Gagal mengambil data ketersediaan tempat tidur (Aplicares)").Cause(err).Build()
	}
	return res, nil
}

func (s *service) CreateBed(ctx context.Context, kdPpk string, req BedData) error {
	err := s.repo.Create(ctx, kdPpk, req)
	if err != nil {
		return errors.ExternalError().Message("Gagal membuat data tempat tidur baru (Aplicares)").Cause(err).Build()
	}
	return nil
}

func (s *service) UpdateBed(ctx context.Context, kdPpk string, req BedData) error {
	err := s.repo.Update(ctx, kdPpk, req)
	if err != nil {
		return errors.ExternalError().Message("Gagal memperbarui data tempat tidur (Aplicares)").Cause(err).Build()
	}
	return nil
}

func (s *service) DeleteBed(ctx context.Context, kdPpk string, req BedDeletePayload) error {
	err := s.repo.Delete(ctx, kdPpk, req)
	if err != nil {
		return errors.ExternalError().Message("Gagal menghapus data tempat tidur (Aplicares)").Cause(err).Build()
	}
	return nil
}
