-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS maquinas (
    id            UUID          PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id     UUID          NOT NULL,
    nome          TEXT          NOT NULL,
    modelo        TEXT          NOT NULL DEFAULT '',
    setor         TEXT          NOT NULL DEFAULT 'geral'
                  CHECK (setor IN ('separacao','corte','bordado','silk','sublimacao','dtf','aplik','costura','acabamento','geral')),
    icone         TEXT          NOT NULL DEFAULT '⚙',
    status        TEXT          NOT NULL DEFAULT 'ok' CHECK (status IN ('ok','manut')),
    pcs_dia       INTEGER       NOT NULL DEFAULT 0,
    obs           TEXT          NOT NULL DEFAULT '',
    prox_manut    DATE,
    motivo_manut  TEXT          NOT NULL DEFAULT '',
    ativo         BOOLEAN       NOT NULL DEFAULT true,
    criado_em     TIMESTAMPTZ   NOT NULL DEFAULT NOW(),
    atualizado_em TIMESTAMPTZ   NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_maquinas_tenant  ON maquinas (tenant_id, ativo, status);
CREATE INDEX IF NOT EXISTS idx_maquinas_setor   ON maquinas (tenant_id, setor, status);
-- +goose StatementEnd

-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS maquinas_manutencao (
    id            UUID          PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id     UUID          NOT NULL,
    maquina_id    UUID          NOT NULL REFERENCES maquinas(id) ON DELETE CASCADE,
    tipo          TEXT          NOT NULL DEFAULT 'preventiva'
                  CHECK (tipo IN ('preventiva','corretiva','emergencial')),
    descricao     TEXT          NOT NULL DEFAULT '',
    responsavel   TEXT          NOT NULL DEFAULT '',
    data_inicio   DATE          NOT NULL DEFAULT CURRENT_DATE,
    data_fim      DATE,
    custo         NUMERIC(10,2) NOT NULL DEFAULT 0,
    observacoes   TEXT          NOT NULL DEFAULT '',
    criado_em     TIMESTAMPTZ   NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_maqmanut_maquina ON maquinas_manutencao (tenant_id, maquina_id, data_inicio DESC);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS maquinas_manutencao;
-- +goose StatementEnd
-- +goose StatementBegin
DROP TABLE IF EXISTS maquinas;
-- +goose StatementEnd
