package db

import (
	"database/sql"
	"fmt"
	"time"

	"deployment-manager/internal/model"
)

type RepoStore struct {
	db *sql.DB
}

func NewRepoStore(db *sql.DB) *RepoStore {
	return &RepoStore{db: db}
}

func (rs *RepoStore) Create(repo *model.Repo) error {
	query := `
		INSERT INTO repos (repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at)
		VALUES (?, ?, ?, ?, ?, ?, ?, ?)
	`
	
	now := time.Now()
	result, err := rs.db.Exec(query, repo.RepoURL, repo.Status, repo.UserID, repo.Type, repo.ImageTag, repo.LastError, now, now)
	if err != nil {
		return fmt.Errorf("failed to create repo: %w", err)
	}
	
	id, err := result.LastInsertId()
	if err != nil {
		return fmt.Errorf("failed to get last insert id: %w", err)
	}
	
	repo.ID = id
	repo.CreatedAt = now
	repo.UpdatedAt = now
	return nil
}

func (rs *RepoStore) Get(id int64) (*model.Repo, error) {
	query := `
		SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
		FROM repos
		WHERE id = ?
	`
	
	repo := &model.Repo{}
	var imageTag, lastError sql.NullString
	
	err := rs.db.QueryRow(query, id).Scan(
		&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
		&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
	)
	
	if err != nil {
		return nil, fmt.Errorf("failed to get repo: %w", err)
	}
	
	if imageTag.Valid {
		repo.ImageTag = &imageTag.String
	}
	if lastError.Valid {
		repo.LastError = &lastError.String
	}
	
	return repo, nil
}

func (rs *RepoStore) Update(repo *model.Repo) error {
	query := `
		UPDATE repos
		SET repo_url = ?, status = ?, user_id = ?, type = ?, image_tag = ?, last_error = ?, updated_at = ?
		WHERE id = ?
	`
	
	repo.UpdatedAt = time.Now()
	result, err := rs.db.Exec(query, repo.RepoURL, repo.Status, repo.UserID, repo.Type, repo.ImageTag, repo.LastError, repo.UpdatedAt, repo.ID)
	if err != nil {
		return fmt.Errorf("failed to update repo: %w", err)
	}
	
	rowsAffected, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("failed to get rows affected: %w", err)
	}
	
	if rowsAffected == 0 {
		return fmt.Errorf("no rows affected, repo not found")
	}
	
	return nil
}

func (rs *RepoStore) Delete(id int64) error {
	query := `DELETE FROM repos WHERE id = ?`
	
	result, err := rs.db.Exec(query, id)
	if err != nil {
		return fmt.Errorf("failed to delete repo: %w", err)
	}
	
	rowsAffected, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("failed to get rows affected: %w", err)
	}
	
	if rowsAffected == 0 {
		return fmt.Errorf("no rows affected, repo not found")
	}
	
	return nil
}

func (rs *RepoStore) ListByStatus(status model.RepoStatus) ([]*model.Repo, error) {
	query := `
		SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
		FROM repos
		WHERE status = ?
		ORDER BY created_at DESC
	`
	
	rows, err := rs.db.Query(query, status)
	if err != nil {
		return nil, fmt.Errorf("failed to list repos by status: %w", err)
	}
	defer rows.Close()
	
	var repos []*model.Repo
	for rows.Next() {
		repo := &model.Repo{}
		var imageTag, lastError sql.NullString
		
		err := rows.Scan(
			&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
			&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
		)
		
		if err != nil {
			return nil, fmt.Errorf("failed to scan repo row: %w", err)
		}
		
		if imageTag.Valid {
			repo.ImageTag = &imageTag.String
		}
		if lastError.Valid {
			repo.LastError = &lastError.String
		}
		
		repos = append(repos, repo)
	}
	
	if err = rows.Err(); err != nil {
		return nil, fmt.Errorf("error iterating repo rows: %w", err)
	}
	
	return repos, nil
}

func (rs *RepoStore) ListByUser(userID string) ([]*model.Repo, error) {
	query := `
		SELECT id, repo_url, status, user_id, type, image_tag, last_error, created_at, updated_at
		FROM repos
		WHERE user_id = ?
		ORDER BY created_at DESC
	`
	
	rows, err := rs.db.Query(query, userID)
	if err != nil {
		return nil, fmt.Errorf("failed to list repos by user: %w", err)
	}
	defer rows.Close()
	
	var repos []*model.Repo
	for rows.Next() {
		repo := &model.Repo{}
		var imageTag, lastError sql.NullString
		
		err := rows.Scan(
			&repo.ID, &repo.RepoURL, &repo.Status, &repo.UserID, &repo.Type,
			&imageTag, &lastError, &repo.CreatedAt, &repo.UpdatedAt,
		)
		
		if err != nil {
			return nil, fmt.Errorf("failed to scan repo row: %w", err)
		}
		
		if imageTag.Valid {
			repo.ImageTag = &imageTag.String
		}
		if lastError.Valid {
			repo.LastError = &lastError.String
		}
		
		repos = append(repos, repo)
	}
	
	if err = rows.Err(); err != nil {
		return nil, fmt.Errorf("error iterating repo rows: %w", err)
	}
	
	return repos, nil
}
