// Copyright 2025 The Bucketeer Authors.
//
// 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 v2

import (
	"context"
	_ "embed"
	"errors"

	"github.com/bucketeer-io/bucketeer/v2/pkg/account/domain"
	pkgErr "github.com/bucketeer-io/bucketeer/v2/pkg/error"
	"github.com/bucketeer-io/bucketeer/v2/pkg/storage/v2/mysql"
	proto "github.com/bucketeer-io/bucketeer/v2/proto/account"
)

var (
	ErrSystemAdminAccountNotFound = pkgErr.NewErrorNotFound(
		pkgErr.AccountPackageName,
		"admin account not found",
		"admin_account",
	)
)

var (
	//go:embed sql/account_v2/select_system_admin_account_v2.sql
	selectSystemAdminAccountV2SQL string
)

func (s *accountStorage) GetSystemAdminAccountV2(ctx context.Context, email string) (*domain.AccountV2, error) {
	account := proto.AccountV2{}
	var organizationRole int32
	err := s.qe.QueryRowContext(
		ctx,
		selectSystemAdminAccountV2SQL,
		email,
	).Scan(
		&account.Email,
		&account.Name,
		&account.FirstName,
		&account.LastName,
		&account.Language,
		&account.AvatarImageUrl,
		&account.AvatarFileType,
		&account.AvatarImage,
		&mysql.JSONObject{Val: &account.Tags},
		&mysql.JSONObject{Val: &account.Teams},
		&account.OrganizationId,
		&organizationRole,
		&mysql.JSONObject{Val: &account.EnvironmentRoles},
		&account.Disabled,
		&account.CreatedAt,
		&account.UpdatedAt,
		&account.LastSeen,
		&mysql.JSONObject{Val: &account.SearchFilters},
	)
	if err != nil {
		if errors.Is(err, mysql.ErrNoRows) {
			return nil, ErrSystemAdminAccountNotFound
		}
		return nil, err
	}
	account.OrganizationRole = proto.AccountV2_Role_Organization(organizationRole)
	return &domain.AccountV2{AccountV2: &account}, nil
}
