# frozen_string_literal: true
# == Schema Information
#
# Table name: services
#
#  id         :integer      not null, primary key
#  name       :string       not null
#  url        :string       not null
#  token      :string
#

class Service < ApplicationRecord
  has_many :routes

  validates :name, format: { with: /([0-9a-zA-Z]{3,60})/ }, presence: true
	validates :url, format: { with: /(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?/ }, presence: true
  validates :token, format: { with: /([0-9a-zA-Z]{6,60})/ }, presence: true

	validates :name, uniqueness: { scope: :url, message: "a service with this name already exists" }
	validates :url, uniqueness: { scope: :name, message: "a service with this url already exists" }
	validates :token, uniqueness: { scope: :url, message: "a service with this token already exists" }
	validates :url, uniqueness: { scope: :token, message: "a service with this url already exists" }

  default_scope { order(:created_at) }
end
