CREATE TABLE IF NOT EXISTS users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  email VARCHAR(190) NOT NULL UNIQUE,
  password_hash VARCHAR(255) NOT NULL,
  name VARCHAR(190) NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS company (
  id INT PRIMARY KEY,
  name VARCHAR(190) NOT NULL,
  address TEXT NULL,
  tax_office VARCHAR(190) NULL,
  tax_number VARCHAR(50) NULL,
  phone VARCHAR(50) NULL,
  email VARCHAR(190) NULL,
  iban VARCHAR(64) NULL,
  website VARCHAR(190) NULL,
  logo_path VARCHAR(255) NULL,
  default_lang VARCHAR(10) NOT NULL DEFAULT 'tr',
  currency_symbol VARCHAR(10) NOT NULL DEFAULT '₺',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO company (id, name, default_lang, currency_symbol)
VALUES (1, 'Firma Adı', 'tr', '₺')
ON DUPLICATE KEY UPDATE id=id;

CREATE TABLE IF NOT EXISTS tax_rates (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  rate DECIMAL(5,2) NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO tax_rates (name, rate, is_active) VALUES
('KDV %1', 1.00, 1),
('KDV %10', 10.00, 1),
('KDV %20', 20.00, 1)
ON DUPLICATE KEY UPDATE name=VALUES(name);

CREATE TABLE IF NOT EXISTS number_sequences (
  id INT AUTO_INCREMENT PRIMARY KEY,
  key_name VARCHAR(50) NOT NULL UNIQUE,
  prefix VARCHAR(50) NOT NULL,
  year INT NOT NULL,
  last_number INT NOT NULL DEFAULT 0,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO number_sequences (key_name, prefix, year, last_number)
VALUES
('quote', 'TEK-', YEAR(CURDATE()), 0),
('invoice', 'FAT-', YEAR(CURDATE()), 0)
ON DUPLICATE KEY UPDATE key_name=key_name;

-- Demo admin kullanıcı (şifre: 123456)
-- Bu hash PHP password_hash ile üretilmiştir.
INSERT INTO users (email, password_hash, name, is_active)
VALUES ('admin@demo.local', '$2y$10$4JcYQY0o4oF0WQzNwqT5pO8o9C9c1rQbJpXx8yY8qQf2yLk4o0rPi', 'Admin', 1)
ON DUPLICATE KEY UPDATE email=email;
