name: Go

on:
  push:
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: "0 0 * * *"

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      GOOSE_DRIVER: postgres
      GOOSE_DBSTRING: "user=feedvault password=feedvault dbname=feedvault sslmode=disable host=localhost port=5432"

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: feedvault
          POSTGRES_PASSWORD: feedvault
          POSTGRES_DB: feedvault

        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          check-latest: true
          go-version: stable

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Install Goose
        run: go install github.com/pressly/goose/v3/cmd/goose@latest

      - name: Goose Status
        run: goose -dir sql/schema status

      - name: Goose Up (Migrate Database)
        run: goose -dir sql/schema up

      - name: Test
        run: go test ./... -v

      - name: Lint
        run: go vet ./...

      - name: Fmt
        run: go fmt ./...

      - name: Tidy
        run: go mod tidy

      - name: Build
        run: go build -v -ldflags="-s -w" .
