import type {BlueprintError} from '../types/errors.js'
import {APPLICATION_VISIBILITIES} from '../types/studios.js'
import {isReference} from '../utils/validation.js'
import {validateResource} from './resources.js'

/** hostname validity pattern */
const STUDIO_SLUG_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/

/**
 * Validates that the given resource is a valid Studio.
 * @param resource The Studio resource
 * @hidden
 * @category Validation
 * @returns A list of validation errors
 */
export function validateStudio(resource: unknown): BlueprintError[] {
  if (!resource) return [{type: 'invalid_value', message: 'Studio config must be provided'}]
  if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Studio config must be an object'}]

  const errors: BlueprintError[] = validateResource(resource, {projectContained: true})

  if ('type' in resource && resource.type !== 'sanity.studio') {
    errors.push({type: 'invalid_value', message: 'Studio type must be `sanity.studio`'})
  }

  if (!('root' in resource) || !resource.root) {
    errors.push({type: 'missing_parameter', message: 'Studio root is required'})
  } else if (typeof resource.root !== 'string') {
    errors.push({type: 'invalid_type', message: 'Studio root must be a string'})
  }

  if ('project' in resource) {
    if (typeof resource.project !== 'string') {
      errors.push({type: 'invalid_type', message: 'Studio project must be a string'})
    }
  }

  if (!('slug' in resource) || !resource.slug) {
    errors.push({type: 'missing_parameter', message: 'Studio slug is required'})
  } else if (typeof resource.slug !== 'string') {
    errors.push({type: 'invalid_type', message: 'Studio slug must be a string'})
  } else if (!isReference(resource.slug) && !STUDIO_SLUG_PATTERN.test(resource.slug)) {
    errors.push({type: 'invalid_format', message: `Studio slug must match pattern: ${STUDIO_SLUG_PATTERN.source}`})
  }

  if (!('title' in resource) || !resource.title) {
    errors.push({type: 'missing_parameter', message: 'Studio title is required'})
  } else if (typeof resource.title !== 'string') {
    errors.push({type: 'invalid_type', message: 'Studio title must be a string'})
  }

  if ('icon' in resource) {
    if (typeof resource.icon !== 'string') {
      errors.push({type: 'invalid_type', message: 'Studio icon must be a string'})
    }
  }

  if (
    'visibility' in resource &&
    typeof resource.visibility !== 'undefined' &&
    !APPLICATION_VISIBILITIES.some((av) => av === resource.visibility)
  ) {
    errors.push({type: 'invalid_value', message: 'visibility must be one of `default`, `unlisted`, or `disabled`'})
  }

  if (!('autoUpdates' in resource) || !resource.autoUpdates) {
    errors.push({type: 'missing_parameter', message: 'Studio autoUpdates is required'})
  } else if (typeof resource.autoUpdates !== 'object') {
    errors.push({type: 'invalid_type', message: 'Studio autoUpdates must be an object'})
  } else {
    if (!('enabled' in resource.autoUpdates)) {
      errors.push({type: 'missing_parameter', message: 'Studio autoUpdates.enabled is required'})
    } else if (typeof resource.autoUpdates.enabled !== 'boolean') {
      errors.push({type: 'invalid_type', message: 'Studio autoUpdates.enabled must be a boolean'})
    }

    if ('version' in resource.autoUpdates) {
      if (typeof resource.autoUpdates.version !== 'string') {
        errors.push({type: 'invalid_type', message: 'Studio autoUpdates.version must be a string'})
      }
    }
  }

  if ('basePath' in resource) {
    if (typeof resource.basePath !== 'string') {
      errors.push({type: 'invalid_type', message: 'Studio basePath must be a string'})
    }
  }

  if ('minify' in resource) {
    if (typeof resource.minify !== 'boolean') {
      errors.push({type: 'invalid_type', message: 'Studio minify must be a boolean'})
    }
  }

  if ('reactCompiler' in resource) {
    if (typeof resource.reactCompiler !== 'boolean') {
      errors.push({type: 'invalid_type', message: 'Studio reactCompiler must be a boolean'})
    }
  }

  if ('sourceMap' in resource) {
    if (typeof resource.sourceMap !== 'boolean') {
      errors.push({type: 'invalid_type', message: 'Studio sourceMap must be a boolean'})
    }
  }

  return errors
}
