package repository

import (
	"context"
	"fmt"

	"apskel-pos-be/internal/entities"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type FileRepository interface {
	Create(ctx context.Context, file *entities.File) error
	GetByID(ctx context.Context, id uuid.UUID) (*entities.File, error)
	GetByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]*entities.File, error)
	GetByUserID(ctx context.Context, userID uuid.UUID) ([]*entities.File, error)
	Update(ctx context.Context, file *entities.File) error
	Delete(ctx context.Context, id uuid.UUID) error
	List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.File, int64, error)
	GetByFileName(ctx context.Context, fileName string) (*entities.File, error)
	ExistsByFileName(ctx context.Context, fileName string) (bool, error)
}

type FileRepositoryImpl struct {
	db *gorm.DB
}

func NewFileRepositoryImpl(db *gorm.DB) *FileRepositoryImpl {
	return &FileRepositoryImpl{
		db: db,
	}
}

func (r *FileRepositoryImpl) Create(ctx context.Context, file *entities.File) error {
	return r.db.WithContext(ctx).Create(file).Error
}

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

func (r *FileRepositoryImpl) GetByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]*entities.File, error) {
	var files []*entities.File
	err := r.db.WithContext(ctx).Where("organization_id = ?", organizationID).Find(&files).Error
	return files, err
}

func (r *FileRepositoryImpl) GetByUserID(ctx context.Context, userID uuid.UUID) ([]*entities.File, error) {
	var files []*entities.File
	err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&files).Error
	return files, err
}

func (r *FileRepositoryImpl) Update(ctx context.Context, file *entities.File) error {
	return r.db.WithContext(ctx).Save(file).Error
}

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

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

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

	// Apply filters
	for key, value := range filters {
		if value != nil && value != "" {
			query = query.Where(fmt.Sprintf("%s = ?", key), value)
		}
	}

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

	// Get paginated results
	err = query.Offset(offset).Limit(limit).Order("created_at DESC").Find(&files).Error
	if err != nil {
		return nil, 0, err
	}

	return files, total, nil
}

func (r *FileRepositoryImpl) GetByFileName(ctx context.Context, fileName string) (*entities.File, error) {
	var file entities.File
	err := r.db.WithContext(ctx).Where("file_name = ?", fileName).First(&file).Error
	if err != nil {
		return nil, err
	}
	return &file, nil
}

func (r *FileRepositoryImpl) ExistsByFileName(ctx context.Context, fileName string) (bool, error) {
	var count int64
	err := r.db.WithContext(ctx).Model(&entities.File{}).Where("file_name = ?", fileName).Count(&count).Error
	return count > 0, err
}
