package postgres

import (
	"context"
	"database/sql"
	"encoding/json"
	"time"

	"github.com/google/uuid"
	"github.com/rede5/gohorsejobs/backend/internal/core/domain/entity"
)

type UserRepository struct {
	db *sql.DB
}

func NewUserRepository(db *sql.DB) *UserRepository {
	return &UserRepository{db: db}
}

func (r *UserRepository) Save(ctx context.Context, user *entity.User) (*entity.User, error) {
	if user.ID == "" {
		user.ID = uuid.New().String()
	}

	tx, err := r.db.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	// Serialize metadata
	metadata, err := json.Marshal(user.Metadata)
	if err != nil {
		return nil, err
	}

	// 1. Insert User
	query := `
		INSERT INTO core_users (id, tenant_id, name, email, password_hash, status, metadata, created_at, updated_at)
		VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
	`
	_, err = tx.ExecContext(ctx, query,
		user.ID,
		user.TenantID,
		user.Name,
		user.Email,
		user.PasswordHash,
		user.Status,
		metadata,
		user.CreatedAt,
		user.UpdatedAt,
	)
	if err != nil {
		return nil, err
	}

	// 2. Insert Roles
	if len(user.Roles) > 0 {
		roleQuery := `INSERT INTO core_user_roles (user_id, role) VALUES ($1, $2)`
		for _, role := range user.Roles {
			_, err := tx.ExecContext(ctx, roleQuery, user.ID, role.Name)
			if err != nil {
				return nil, err
			}
		}
	}

	if err := tx.Commit(); err != nil {
		return nil, err
	}

	return user, nil
}

func (r *UserRepository) FindByEmail(ctx context.Context, email string) (*entity.User, error) {
	query := `SELECT id, tenant_id, name, email, password_hash, status, created_at, updated_at FROM core_users WHERE email = $1`
	row := r.db.QueryRowContext(ctx, query, email)

	u := &entity.User{}
	err := row.Scan(&u.ID, &u.TenantID, &u.Name, &u.Email, &u.PasswordHash, &u.Status, &u.CreatedAt, &u.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil // Return nil if not found
		}
		return nil, err
	}

	u.Roles, _ = r.getRoles(ctx, u.ID)
	return u, nil
}

func (r *UserRepository) FindByID(ctx context.Context, id string) (*entity.User, error) {
	query := `SELECT id, tenant_id, name, email, password_hash, status, created_at, updated_at FROM core_users WHERE id = $1`
	row := r.db.QueryRowContext(ctx, query, id)

	u := &entity.User{}
	err := row.Scan(&u.ID, &u.TenantID, &u.Name, &u.Email, &u.PasswordHash, &u.Status, &u.CreatedAt, &u.UpdatedAt)
	if err != nil {
		return nil, err
	}

	u.Roles, _ = r.getRoles(ctx, u.ID)
	return u, nil
}

func (r *UserRepository) FindAllByTenant(ctx context.Context, tenantID string, limit, offset int) ([]*entity.User, int, error) {
	var total int
	countQuery := `SELECT COUNT(*) FROM core_users WHERE tenant_id = $1`
	if err := r.db.QueryRowContext(ctx, countQuery, tenantID).Scan(&total); err != nil {
		return nil, 0, err
	}

	query := `SELECT id, tenant_id, name, email, password_hash, status, created_at, updated_at
		FROM core_users
		WHERE tenant_id = $1
		ORDER BY created_at DESC
		LIMIT $2 OFFSET $3`
	rows, err := r.db.QueryContext(ctx, query, tenantID, limit, offset)
	if err != nil {
		return nil, 0, err
	}
	defer rows.Close()

	var users []*entity.User
	for rows.Next() {
		u := &entity.User{}
		if err := rows.Scan(&u.ID, &u.TenantID, &u.Name, &u.Email, &u.PasswordHash, &u.Status, &u.CreatedAt, &u.UpdatedAt); err != nil {
			return nil, 0, err
		}
		// Populate roles N+1? Ideally join, but for now simple
		u.Roles, _ = r.getRoles(ctx, u.ID)
		users = append(users, u)
	}
	return users, total, nil
}

func (r *UserRepository) Update(ctx context.Context, user *entity.User) (*entity.User, error) {
	// Not fully implemented for roles update for brevity, just fields
	user.UpdatedAt = time.Now()
	query := `UPDATE core_users SET name=$1, email=$2, status=$3, updated_at=$4 WHERE id=$5`
	_, err := r.db.ExecContext(ctx, query, user.Name, user.Email, user.Status, user.UpdatedAt, user.ID)
	return user, err
}

func (r *UserRepository) Delete(ctx context.Context, id string) error {
	_, err := r.db.ExecContext(ctx, `DELETE FROM core_users WHERE id=$1`, id)
	return err
}

func (r *UserRepository) getRoles(ctx context.Context, userID string) ([]entity.Role, error) {
	rows, err := r.db.QueryContext(ctx, `SELECT role FROM core_user_roles WHERE user_id = $1`, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var roles []entity.Role
	for rows.Next() {
		var roleName string
		rows.Scan(&roleName)
		roles = append(roles, entity.Role{Name: roleName})
	}
	return roles, nil
}
