//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package dao

import (
	"context"
	"ragflow/internal/entity"

	"gorm.io/gorm"
)

// SkillSpaceDAO data access object for skills space
type SkillSpaceDAO struct{}

// NewSkillSpaceDAO creates a new SkillSpaceDAO
func NewSkillSpaceDAO() *SkillSpaceDAO {
	return &SkillSpaceDAO{}
}

// Create creates a new skills space
func (dao *SkillSpaceDAO) Create(ctx context.Context, db *gorm.DB, space *entity.SkillSpace) error {
	return db.WithContext(ctx).Create(space).Error
}

// GetByID retrieves a skills space by ID (active only)
func (dao *SkillSpaceDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.SkillSpace, error) {
	var space entity.SkillSpace
	err := db.WithContext(ctx).Where("id = ? AND status = ?", id, entity.SpaceStatusActive).First(&space).Error
	if err != nil {
		return nil, err
	}
	return &space, nil
}

// GetByTenantID retrieves all skills spaces by tenant ID (active only)
func (dao *SkillSpaceDAO) GetByTenantID(ctx context.Context, db *gorm.DB, tenantID string) ([]*entity.SkillSpace, error) {
	var spaces []*entity.SkillSpace
	err := db.WithContext(ctx).Where("tenant_id = ? AND status = ?", tenantID, entity.SpaceStatusActive).Order("create_time DESC").Find(&spaces).Error
	return spaces, err
}

// GetByTenantAndName retrieves a skills space by tenant ID and name (active only)
func (dao *SkillSpaceDAO) GetByTenantAndName(ctx context.Context, db *gorm.DB, tenantID, name string) (*entity.SkillSpace, error) {
	var space entity.SkillSpace
	err := db.WithContext(ctx).Where("tenant_id = ? AND name = ? AND status = ?", tenantID, name, entity.SpaceStatusActive).First(&space).Error
	if err != nil {
		return nil, err
	}
	return &space, nil
}

// GetByTenantAndNameAnyStatus retrieves a skills space by tenant ID and name regardless of status
func (dao *SkillSpaceDAO) GetByTenantAndNameAnyStatus(ctx context.Context, db *gorm.DB, tenantID, name string) (*entity.SkillSpace, error) {
	var space entity.SkillSpace
	err := db.WithContext(ctx).Where("tenant_id = ? AND name = ?", tenantID, name).First(&space).Error
	if err != nil {
		return nil, err
	}
	return &space, nil
}

// GetByIDAnyStatus retrieves a skills space by ID regardless of status
func (dao *SkillSpaceDAO) GetByIDAnyStatus(ctx context.Context, db *gorm.DB, id string) (*entity.SkillSpace, error) {
	var space entity.SkillSpace
	err := db.WithContext(ctx).Where("id = ?", id).First(&space).Error
	if err != nil {
		return nil, err
	}
	return &space, nil
}

// GetByFolderID retrieves a skills space by folder ID (active only)
func (dao *SkillSpaceDAO) GetByFolderID(ctx context.Context, db *gorm.DB, folderID string) (*entity.SkillSpace, error) {
	var space entity.SkillSpace
	err := db.WithContext(ctx).Where("folder_id = ? AND status = ?", folderID, entity.SpaceStatusActive).First(&space).Error
	if err != nil {
		return nil, err
	}
	return &space, nil
}

// Update updates a skills space
func (dao *SkillSpaceDAO) Update(ctx context.Context, db *gorm.DB, space *entity.SkillSpace) error {
	return db.WithContext(ctx).Save(space).Error
}

// UpdateByID updates skills space by ID
func (dao *SkillSpaceDAO) UpdateByID(ctx context.Context, db *gorm.DB, id string, updates map[string]interface{}) error {
	return db.WithContext(ctx).Model(&entity.SkillSpace{}).Where("id = ?", id).Updates(updates).Error
}

// Delete deletes a skills space by ID (soft delete)
func (dao *SkillSpaceDAO) Delete(ctx context.Context, db *gorm.DB, id string) error {
	return db.WithContext(ctx).Model(&entity.SkillSpace{}).Where("id = ?", id).Update("status", entity.SpaceStatusDeleted).Error
}

// CASStatus performs a compare-and-swap on the space status atomically
// Returns true if the update was applied, false if the current status didn't match expected
func (dao *SkillSpaceDAO) CASStatus(ctx context.Context, db *gorm.DB, id string, expectedStatus, newStatus string) (bool, error) {
	result := db.WithContext(ctx).Model(&entity.SkillSpace{}).
		Where("id = ? AND status = ?", id, expectedStatus).
		Update("status", newStatus)
	if result.Error != nil {
		return false, result.Error
	}
	return result.RowsAffected > 0, nil
}

// DeletePermanentByName permanently deletes a skills space by tenant ID and name
// This is used to clean up previously deleted spaces (only deletes status='0' deleted spaces, NOT deleting spaces)
func (dao *SkillSpaceDAO) DeletePermanentByName(ctx context.Context, db *gorm.DB, tenantID, name string) error {
	return db.WithContext(ctx).Unscoped().Where("tenant_id = ? AND name = ? AND status = ?", tenantID, name, entity.SpaceStatusDeleted).Delete(&entity.SkillSpace{}).Error
}

// CountByTenant counts skills spaces by tenant ID
func (dao *SkillSpaceDAO) CountByTenant(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
	var count int64
	err := db.WithContext(ctx).Model(&entity.SkillSpace{}).Where("tenant_id = ? AND status = ?", tenantID, entity.SpaceStatusActive).Count(&count).Error
	return count, err
}
