package postgres

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

	"github.com/lib/pq"
	"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) LinkGuestApplications(ctx context.Context, email string, userID string) error {
	query := `
		UPDATE applications
		SET user_id = $1
		WHERE email = $2 AND (user_id IS NULL OR user_id LIKE 'guest_%')
	`
	_, err := r.db.ExecContext(ctx, query, userID, email)
	return err
}

func (r *UserRepository) Save(ctx context.Context, user *entity.User) (*entity.User, error) {
	tx, err := r.db.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	// TenantID is string (UUID) or empty
	var tenantID *string
	if user.TenantID != "" {
		tenantID = &user.TenantID
	}

	// 1. Insert User
	query := `
		INSERT INTO users (
			identifier, password_hash, role, full_name, email, name, tenant_id, status, created_at, updated_at, avatar_url,
			phone, bio, address, city, state, zip_code, birth_date, education, experience, skills, objective, title
		)
		VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)
		RETURNING id
	`

	var id string
	// Map the first role to the role column, default to 'candidate'
	role := "candidate"
	if len(user.Roles) > 0 {
		role = user.Roles[0].Name
	}

	// Prepare pq Array for skills
	// IMPORTANT: import "github.com/lib/pq" needed at top

	err = tx.QueryRowContext(ctx, query,
		user.Email, // identifier = email
		user.PasswordHash,
		role,
		user.Name,
		user.Email,
		user.Name,
		tenantID,
		user.Status,
		user.CreatedAt,
		user.UpdatedAt,
		user.AvatarUrl,
		user.Phone,
		user.Bio,
		user.Address,
		user.City,
		user.State,
		user.ZipCode,
		user.BirthDate,
		user.Education,
		user.Experience,
		pq.Array(user.Skills),
		user.Objective,
		user.Title,
	).Scan(&id)

	if err != nil {
		return nil, err
	}
	user.ID = id

	// 2. Insert Roles into user_roles table
	if len(user.Roles) > 0 {
		roleQuery := `INSERT INTO user_roles (user_id, role) VALUES ($1, $2) ON CONFLICT DO NOTHING`
		for _, role := range user.Roles {
			_, err := tx.ExecContext(ctx, roleQuery, 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, COALESCE(tenant_id::text, ''), COALESCE(name, full_name, ''),
		COALESCE(email, identifier), password_hash, COALESCE(status, 'active'), created_at, updated_at, COALESCE(avatar_url, ''),
		phone, bio, address, city, state, zip_code, birth_date, education, experience, skills, objective, title
		FROM users WHERE email = $1 OR identifier = $1`
	row := r.db.QueryRowContext(ctx, query, email)

	u := &entity.User{}
	var dbID string
	var phone sql.NullString
	var bio sql.NullString
	err := row.Scan(
		&dbID,
		&u.TenantID,
		&u.Name,
		&u.Email,
		&u.PasswordHash,
		&u.Status,
		&u.CreatedAt,
		&u.UpdatedAt,
		&u.AvatarUrl,
		&phone,
		&bio,
		&u.Address,
		&u.City,
		&u.State,
		&u.ZipCode,
		&u.BirthDate,
		&u.Education,
		&u.Experience,
		pq.Array(&u.Skills),
		&u.Objective,
		&u.Title,
	)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil // Return nil if not found
		}
		return nil, err
	}
	u.ID = dbID
	u.Phone = nullStringPtr(phone)
	u.Bio = nullStringPtr(bio)
	u.Roles, _ = r.getRoles(ctx, dbID)
	return u, nil
}

func (r *UserRepository) FindByID(ctx context.Context, id string) (*entity.User, error) {
	query := `SELECT id, COALESCE(tenant_id::text, ''), COALESCE(name, full_name, ''),
		COALESCE(email, identifier), password_hash, COALESCE(status, 'active'), created_at, updated_at, COALESCE(avatar_url, ''),
		phone, bio, address, city, state, zip_code, birth_date, education, experience, skills, objective, title
		FROM users WHERE id = $1`
	row := r.db.QueryRowContext(ctx, query, id)

	u := &entity.User{}
	var dbID string
	var phone sql.NullString
	var bio sql.NullString
	err := row.Scan(
		&dbID,
		&u.TenantID,
		&u.Name,
		&u.Email,
		&u.PasswordHash,
		&u.Status,
		&u.CreatedAt,
		&u.UpdatedAt,
		&u.AvatarUrl,
		&phone,
		&bio,
		&u.Address,
		&u.City,
		&u.State,
		&u.ZipCode,
		&u.BirthDate,
		&u.Education,
		&u.Experience,
		pq.Array(&u.Skills),
		&u.Objective,
		&u.Title,
	)
	if err != nil {
		return nil, err
	}
	u.ID = dbID
	u.Phone = nullStringPtr(phone)
	u.Bio = nullStringPtr(bio)
	u.Roles, _ = r.getRoles(ctx, dbID)
	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 users WHERE tenant_id = $1`
	if err := r.db.QueryRowContext(ctx, countQuery, tenantID).Scan(&total); err != nil {
		return nil, 0, err
	}

	query := `SELECT id, COALESCE(tenant_id::text, ''), COALESCE(name, full_name, ''),
		COALESCE(email, identifier), password_hash, COALESCE(status, 'active'), created_at, updated_at, COALESCE(avatar_url, ''),
		phone, bio, address, city, state, zip_code, birth_date, education, experience, skills, objective, title
		FROM 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{}
		var dbID string
		var phone sql.NullString
		var bio sql.NullString
		if err := rows.Scan(
			&dbID,
			&u.TenantID,
			&u.Name,
			&u.Email,
			&u.PasswordHash,
			&u.Status,
			&u.CreatedAt,
			&u.UpdatedAt,
			&u.AvatarUrl,
			&phone,
			&bio,
			&u.Address,
			&u.City,
			&u.State,
			&u.ZipCode,
			&u.BirthDate,
			&u.Education,
			&u.Experience,
			pq.Array(&u.Skills),
			&u.Objective,
			&u.Title,
		); err != nil {
			return nil, 0, err
		}
		u.ID = dbID
		u.Phone = nullStringPtr(phone)
		u.Bio = nullStringPtr(bio)
		u.Roles, _ = r.getRoles(ctx, dbID)
		users = append(users, u)
	}
	return users, total, nil
}

func (r *UserRepository) Update(ctx context.Context, user *entity.User) (*entity.User, error) {
	user.UpdatedAt = time.Now()

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

	// 1. Update basic fields + legacy role column
	// We use the first role as the "legacy" role for compatibility
	primaryRole := ""
	if len(user.Roles) > 0 {
		primaryRole = user.Roles[0].Name
	}

	query := `UPDATE users SET name=$1, full_name=$2, email=$3, status=$4, role=$5, updated_at=$6, avatar_url=$7, phone=$8, bio=$9, password_hash=$10 WHERE id=$11`
	_, err = tx.ExecContext(
		ctx,
		query,
		user.Name,
		user.Name,
		user.Email,
		user.Status,
		primaryRole,
		user.UpdatedAt,
		user.AvatarUrl,
		user.Phone,
		user.Bio,
		user.PasswordHash,
		user.ID,
	)
	if err != nil {
		return nil, err
	}

	// 2. Update user_roles (Delete all and re-insert)
	_, err = tx.ExecContext(ctx, `DELETE FROM user_roles WHERE user_id=$1`, user.ID)
	if err != nil {
		return nil, err
	}

	if len(user.Roles) > 0 {
		stmt, err := tx.PrepareContext(ctx, `INSERT INTO user_roles (user_id, role) VALUES ($1, $2)`)
		if err != nil {
			return nil, err
		}
		defer stmt.Close()

		for _, role := range user.Roles {
			if _, err := stmt.ExecContext(ctx, user.ID, role.Name); err != nil {
				return nil, err
			}
		}
	}

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

	return user, nil
}

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

func (r *UserRepository) getRoles(ctx context.Context, userID string) ([]entity.Role, error) {
	// Query both user_roles table AND legacy role column from users table
	// This ensures backward compatibility with users who have role set in users.role
	query := `
		SELECT role FROM user_roles WHERE user_id = $1
		UNION
		SELECT role FROM users WHERE id = $1 AND role IS NOT NULL AND role != ''
	`
	rows, err := r.db.QueryContext(ctx, query, 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
}

func nullStringPtr(value sql.NullString) *string {
	if value.Valid {
		return &value.String
	}
	return nil
}
