-- KAS Field Sales: new tables only. Your existing `products` table is used as-is.
-- Names are prefixed to avoid clashing with PSI / catalogue tables.

CREATE TABLE IF NOT EXISTS kas_regions (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  code VARCHAR(20) NOT NULL UNIQUE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS kas_agents (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(120) NOT NULL,
  email VARCHAR(150) NULL UNIQUE,
  phone VARCHAR(20) NULL UNIQUE,          -- local format 06XXXXXXXX
  password_hash VARCHAR(255) NOT NULL,
  region_id INT UNSIGNED NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (region_id) REFERENCES kas_regions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS kas_customers (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  region_id INT UNSIGNED NOT NULL,
  name VARCHAR(150) NOT NULL,
  shop_name VARCHAR(150) NULL,
  phone VARCHAR(20) NULL,                 -- local format 06XXXXXXXX
  address VARCHAR(255) NULL,
  city VARCHAR(100) NULL,
  is_walk_in TINYINT(1) NOT NULL DEFAULT 0,
  created_by INT UNSIGNED NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_region_phone (region_id, phone),
  KEY idx_region_name (region_id, name),
  KEY idx_phone (phone),
  FOREIGN KEY (region_id) REFERENCES kas_regions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS kas_invoice_schemes (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(80) NOT NULL,
  prefix VARCHAR(30) NOT NULL,
  next_number INT UNSIGNED NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS kas_sales (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  invoice_no VARCHAR(50) NOT NULL UNIQUE,
  agent_id INT UNSIGNED NOT NULL,
  region_id INT UNSIGNED NOT NULL,
  customer_id INT UNSIGNED NOT NULL,
  sale_date DATETIME NOT NULL,
  status ENUM('final','draft','proforma') NOT NULL DEFAULT 'final',
  payment_term_days INT UNSIGNED NOT NULL DEFAULT 0,
  invoice_scheme_id INT UNSIGNED NOT NULL,
  subtotal DECIMAL(12,2) NOT NULL DEFAULT 0,
  discount_total DECIMAL(12,2) NOT NULL DEFAULT 0,
  total DECIMAL(12,2) NOT NULL DEFAULT 0,
  attachment_path VARCHAR(255) NULL,
  notes TEXT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY idx_agent_date (agent_id, sale_date),
  KEY idx_region (region_id),
  FOREIGN KEY (customer_id) REFERENCES kas_customers(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS kas_sale_lines (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  sale_id INT UNSIGNED NOT NULL,
  product_id INT NOT NULL,                -- matches products.id (adjust type if yours differs)
  product_model VARCHAR(255) NOT NULL,    -- snapshot
  quantity INT UNSIGNED NOT NULL,
  unit_price DECIMAL(12,2) NOT NULL,      -- snapshot of products.retail_price
  discount_type ENUM('fixed','percent') NOT NULL DEFAULT 'fixed',
  discount_value DECIMAL(12,2) NOT NULL DEFAULT 0,
  line_total DECIMAL(12,2) NOT NULL,
  FOREIGN KEY (sale_id) REFERENCES kas_sales(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed data ---------------------------------------------------------------
INSERT INTO kas_regions (name, code) VALUES
 ('Marrakech-Safi','MAR'), ('Casablanca-Settat','CAS'), ('Rabat-Salé-Kénitra','RAB');

INSERT INTO kas_invoice_schemes (name, prefix, next_number) VALUES ('Facture-jm','JM-',1);

-- One Walk-In customer per region
INSERT INTO kas_customers (region_id, name, is_walk_in)
SELECT id, 'Walk-In Customer', 1 FROM kas_regions;

-- Example agent (password: ChangeMe123 -> REPLACE: generate with php -r "echo password_hash('YourPass', PASSWORD_DEFAULT);")
-- INSERT INTO kas_agents (name, email, phone, password_hash, region_id)
-- VALUES ('Agent Marrakech','agent1@example.com','0612345678','<paste hash>',1);
