CREATE DATABASE IF NOT EXISTS milk_collection_db;
USE milk_collection_db;

CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    full_name VARCHAR(100) NOT NULL,
    role ENUM('collector', 'owner') NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    address VARCHAR(255),
    phone VARCHAR(20),
    assigned_collector_id INT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (assigned_collector_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS collections (
    id INT AUTO_INCREMENT PRIMARY KEY,
    collector_id INT NOT NULL,
    customer_id INT NOT NULL,
    amount_ml INT NOT NULL,
    collection_date DATE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (collector_id) REFERENCES users(id),
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

-- Insert default owner user (username: admin, password: admin123)
INSERT INTO users (username, password, full_name, role) 
VALUES ('admin', '$2y$10$vI8aWBnW3fID.ZQ4/zo1G.q1lRps.9cGLcZEiGDMVr5yUP1KUOYTa', 'Sistem Yöneticisi', 'owner');

-- Insert sample collector (username: toplayici1, password: 123456)
INSERT INTO users (username, password, full_name, role) 
VALUES ('toplayici1', '$2y$10$6ixcpMQOqzyKSqN9.vJMl.qPOt2DQCY2Xq3r.hL.6JLJLBfKHvGrO', 'Ahmet Yılmaz', 'collector');

-- Insert sample customers (ilk ikisi toplayici1'e atanmış)
INSERT INTO customers (name, address, phone, assigned_collector_id) VALUES 
('Mehmet Demir Çiftliği', 'Bahçelievler Mah. No:45', '0532 123 4567', 2),
('Ayşe Kaya Mandırası', 'Merkez Mah. No:78', '0543 234 5678', 2),
('Fatma Yıldız Çiftliği', 'Yeni Mah. No:23', '0555 345 6789', NULL);
