-- Tabelas para Técnicos e histórico de serviços
CREATE TABLE IF NOT EXISTS technicians (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  nome VARCHAR(120) NOT NULL,
  cpf VARCHAR(14) NOT NULL,
  rg VARCHAR(20) NOT NULL,
  email VARCHAR(160) NULL,
  telefone VARCHAR(40) NOT NULL,
  bank_name VARCHAR(80) NULL,
  bank_agencia VARCHAR(20) NULL,
  bank_conta VARCHAR(30) NULL,
  pix_key VARCHAR(160) NULL,
  comissao_percent DECIMAL(5,2) DEFAULT 0.00,
  enabled TINYINT(1) DEFAULT 1,
  pdv_code VARCHAR(32) UNIQUE NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS technician_service_history (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  technician_id BIGINT UNSIGNED NOT NULL,
  service_id BIGINT UNSIGNED NULL,
  description VARCHAR(255) NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  commission_amount DECIMAL(12,2) NOT NULL,
  executed_at DATETIME NOT NULL,
  source VARCHAR(32) DEFAULT 'servico',
  reference_id VARCHAR(64) NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_tech_service_history_tech FOREIGN KEY (technician_id) REFERENCES technicians(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;