package repository

import (
	"context"
	"github.com/google/uuid"

	"apskel-pos-be/internal/entities"

	"gorm.io/gorm"
)

type OrganizationRepositoryImpl struct {
	db *gorm.DB
}

func NewOrganizationRepositoryImpl(db *gorm.DB) *OrganizationRepositoryImpl {
	return &OrganizationRepositoryImpl{
		db: db,
	}
}

func (r *OrganizationRepositoryImpl) Create(ctx context.Context, org *entities.Organization) error {
	return r.db.WithContext(ctx).Create(org).Error
}

func (r *OrganizationRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.Organization, error) {
	var org entities.Organization
	err := r.db.WithContext(ctx).First(&org, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &org, nil
}

func (r *OrganizationRepositoryImpl) GetWithOutlets(ctx context.Context, id uuid.UUID) (*entities.Organization, error) {
	var org entities.Organization
	err := r.db.WithContext(ctx).Preload("Outlets").First(&org, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &org, nil
}

func (r *OrganizationRepositoryImpl) GetByPlanType(ctx context.Context, planType string) ([]*entities.Organization, error) {
	var organizations []*entities.Organization
	err := r.db.WithContext(ctx).Where("plan_type = ?", planType).Find(&organizations).Error
	return organizations, err
}

func (r *OrganizationRepositoryImpl) UpdatePlanType(ctx context.Context, id uuid.UUID, planType string) error {
	return r.db.WithContext(ctx).Model(&entities.Organization{}).
		Where("id = ?", id).
		Update("plan_type", planType).Error
}

func (r *OrganizationRepositoryImpl) Update(ctx context.Context, org *entities.Organization) error {
	return r.db.WithContext(ctx).Save(org).Error
}

func (r *OrganizationRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.Organization{}, "id = ?", id).Error
}

func (r *OrganizationRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Organization, int64, error) {
	var organizations []*entities.Organization
	var total int64

	query := r.db.WithContext(ctx).Model(&entities.Organization{})

	for key, value := range filters {
		query = query.Where(key+" = ?", value)
	}

	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	err := query.Limit(limit).Offset(offset).Find(&organizations).Error
	return organizations, total, err
}

func (r *OrganizationRepositoryImpl) Count(ctx context.Context, filters map[string]interface{}) (int64, error) {
	var count int64
	query := r.db.WithContext(ctx).Model(&entities.Organization{})

	for key, value := range filters {
		query = query.Where(key+" = ?", value)
	}

	err := query.Count(&count).Error
	return count, err
}

func (r *OrganizationRepositoryImpl) GetByEmail(ctx context.Context, email string) (*entities.Organization, error) {
	var org entities.Organization
	err := r.db.WithContext(ctx).First(&org, "email = ?", email).Error
	if err != nil {
		return nil, err
	}
	return &org, nil
}
