package test

import (
	"net/http"
	"testing"

	"github.com/gofiber/fiber/v2"
)

func TestWarehouseIntegration(t *testing.T) {
	app, db := setupIntegrationApp(t)
	areaID := createArea(t, app, "Warehouse Area")
	locationID := createLocation(t, app, "Location WH", "Addr", areaID)
	kandangID := createKandang(t, app, "Kandang WH", locationID, 1)

	t.Run("type AREA only needs area_id", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":    "WH Area",
			"type":    "AREA",
			"area_id": areaID,
		})
		if resp.StatusCode != fiber.StatusCreated {
			t.Fatalf("expected 201, got %d: %s", resp.StatusCode, string(body))
		}
	})

	t.Run("type AREA rejects location_id", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":        "WH Area Invalid",
			"type":        "AREA",
			"area_id":     areaID,
			"location_id": locationID,
		})
		if resp.StatusCode != fiber.StatusBadRequest {
			t.Fatalf("expected 400, got %d: %s", resp.StatusCode, string(body))
		}
	})

	t.Run("type LOKASI requires location_id", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":    "WH Lokasi Fail",
			"type":    "LOKASI",
			"area_id": areaID,
		})
		if resp.StatusCode != fiber.StatusBadRequest {
			t.Fatalf("expected 400, got %d: %s", resp.StatusCode, string(body))
		}
	})

	t.Run("type LOKASI success", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":        "WH Lokasi",
			"type":        "LOKASI",
			"area_id":     areaID,
			"location_id": locationID,
		})
		if resp.StatusCode != fiber.StatusCreated {
			t.Fatalf("expected 201, got %d: %s", resp.StatusCode, string(body))
		}
		whID := parseID(t, body)
		wh := fetchWarehouse(t, db, whID)
		if wh.LocationId == nil || *wh.LocationId != locationID {
			t.Fatalf("expected location_id %d, got %v", locationID, wh.LocationId)
		}
	})

	t.Run("type KANDANG requires all ids", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":        "WH Kandang Fail",
			"type":        "KANDANG",
			"area_id":     areaID,
			"location_id": locationID,
		})
		if resp.StatusCode != fiber.StatusBadRequest {
			t.Fatalf("expected 400, got %d: %s", resp.StatusCode, string(body))
		}
	})

	t.Run("type KANDANG success", func(t *testing.T) {
		resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/warehouses", map[string]any{
			"name":        "WH Kandang",
			"type":        "KANDANG",
			"area_id":     areaID,
			"location_id": locationID,
			"kandang_id":  kandangID,
		})
		if resp.StatusCode != fiber.StatusCreated {
			t.Fatalf("expected 201, got %d: %s", resp.StatusCode, string(body))
		}
		whID := parseID(t, body)
		wh := fetchWarehouse(t, db, whID)
		if wh.KandangId == nil || *wh.KandangId != kandangID {
			t.Fatalf("expected kandang_id %d, got %v", kandangID, wh.KandangId)
		}
	})
}
