-- +goose Up
ALTER TABLE file_files
  MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT 'uploaded',
  ADD COLUMN processing_started_at DATETIME(6) NULL AFTER status,
  ADD COLUMN processed_at DATETIME(6) NULL AFTER processing_started_at,
  ADD COLUMN validated_at DATETIME(6) NULL AFTER processed_at,
  ADD COLUMN failed_at DATETIME(6) NULL AFTER validated_at,
  ADD COLUMN failure_reason TEXT NULL AFTER failed_at;

UPDATE file_files
SET status = 'validated',
    processed_at = COALESCE(processed_at, updated_at),
    validated_at = COALESCE(validated_at, updated_at)
WHERE status = 'pending';

UPDATE file_files
SET status = 'processing',
    processing_started_at = COALESCE(processing_started_at, updated_at)
WHERE status = 'uploading';

UPDATE file_files
SET failure_reason = NULLIF(TRIM(upload_error), '')
WHERE (failure_reason IS NULL OR failure_reason = '')
  AND upload_error IS NOT NULL
  AND TRIM(upload_error) <> '';

-- +goose Down
UPDATE file_files
SET status = 'pending'
WHERE status IN ('uploaded', 'validated');

UPDATE file_files
SET status = 'uploading'
WHERE status = 'processing';

ALTER TABLE file_files
  DROP COLUMN failure_reason,
  DROP COLUMN failed_at,
  DROP COLUMN validated_at,
  DROP COLUMN processed_at,
  DROP COLUMN processing_started_at,
  MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT 'pending';
