# --- Frontend processes
local_resource(
  'yarn install',
  cmd='yarn install',
  deps=[
    'yarn.lock',
  ],
  labels=["local"]
)

local_resource(
  'yarn start',
  cmd='rm -rf public/build/assets-manifest.json',
  serve_cmd='yarn start:noLint',
  resource_deps=['yarn install'],

  # Note: this doesn't seem to work as expected - the assets-manifest is somehow created before
  # the webpack build is complete?
  readiness_probe=probe(
    initial_delay_secs=10,
    period_secs=1,
    exec=exec_action(["bash", "-c", "stat public/build/assets-manifest.json"]),
  ),
  allow_parallel=True,
  labels=["local"]
)

local_resource(
  'backend-build',
  "bash ./build-grafana.sh",
  deps=[
    '../../pkg',
    '../../apps',
    '../../kinds',
    '../../kindsv2',
    '../../local',
    '../../scripts',
    '../../conf',
    '../../go.sum',
    '../../go.mod',
  ],
  allow_parallel=True,
  labels=["local"]
)

# --- Docker Compose
docker_compose("./docker-compose.yaml")
dc_resource("proxy",
  resource_deps=["grafana-api", "frontend-service"],
  labels=["services"]
)
dc_resource("grafana-api",
  resource_deps=["yarn start", "backend-build"],
  labels=["services"]
)
dc_resource("frontend-service",
  resource_deps=["yarn start", "backend-build"],
  labels=["services"],
)
dc_resource("alloy", labels=["observability"])
dc_resource("prometheus", labels=["observability"])
dc_resource("loki", labels=["observability"])
dc_resource("tempo", labels=["observability"])

dc_resource("postgres", labels=["misc"])
dc_resource("tempo-init", labels=["misc"])

# paths in tilt files are confusing....
# - if tilt is dealing the the path, it is relative to the Tiltfile
# - if docker is dealing with the path, it is relative to the context
docker_build('grafana-fs-dev',
  # Set the docker context to the root of the repo
  context='../..',
  dockerfile='grafana-fs-dev.dockerfile',

  # Paths relative to the docker context (root of the repo)
  only=[
    'devenv/frontend-service/build/grafana',
    'devenv/frontend-service/provisioning',
    'devenv/frontend-service/configs/grafana-api.local.ini',
    'devenv/frontend-service/configs/frontend-service.local.ini',
    'conf/defaults.ini',
    'public/emails',
    'public/views',
    'public/dashboards',
    'public/app/plugins',
    'public/build/assets-manifest.json',
    'public/gazetteer',
    'public/maps',
    'public/img/bg',
    'public/img/icons',
  ],

  # Sync paths are relative to the Tiltfile
  live_update = [
    sync('./build/grafana', '/grafana/bin/grafana'),
    sync('../../conf/defaults.ini', '/grafana/conf/defaults.ini'),
    sync('../../public/emails', '/grafana/public/emails'),
    sync('../../public/views', '/grafana/public/views'),
    sync('../../public/dashboards', '/grafana/public/dashboards'),
    sync('../../public/app/plugins', '/grafana/public/app/plugins'),
    sync('../../public/build/assets-manifest.json', '/grafana/public/build/assets-manifest.json'),
    sync('./provisioning', '/ignore/provisioning'),  # Just to trigger a restart instead of rebuild
    sync('./configs/grafana-api.local.ini', '/ignore/grafana-api.local.ini'),  # Just to trigger a restart instead of rebuild
    sync('./configs/frontend-service.local.ini', '/ignore/frontend-service.local.ini'),  # Just to trigger a restart instead of rebuild
    restart_container()
  ]
)

docker_build('grafana-proxy',
  # Set the docker context to this frontend-service folder
  context='.',
  dockerfile='proxy.dockerfile',

  # Path relative to the docker context (this folder)
  only=[
    "./configs/nginx.conf",
  ],
  live_update = [
    sync('./configs/nginx.conf', '/etc/nginx/conf.d/default.conf'),
    restart_container()
  ]
)
